|
@@ -41,11 +41,11 @@ Fixture::Fixture(Body *body, Shape *shape, float density)
|
|
|
: body(body)
|
|
|
, fixture(nullptr)
|
|
|
{
|
|
|
- data = new fixtureudata();
|
|
|
- data->ref = nullptr;
|
|
|
+ udata = new fixtureudata();
|
|
|
+ udata->ref = nullptr;
|
|
|
b2FixtureDef def;
|
|
|
def.shape = shape->shape;
|
|
|
- def.userData = (void *)data;
|
|
|
+ def.userData = (void *)udata;
|
|
|
def.density = density;
|
|
|
fixture = body->body->CreateFixture(&def);
|
|
|
this->retain();
|
|
@@ -55,7 +55,7 @@ Fixture::Fixture(Body *body, Shape *shape, float density)
|
|
|
Fixture::Fixture(b2Fixture *f)
|
|
|
: fixture(f)
|
|
|
{
|
|
|
- data = (fixtureudata *)f->GetUserData();
|
|
|
+ udata = (fixtureudata *)f->GetUserData();
|
|
|
body = (Body *)Memoizer::find(f->GetBody());
|
|
|
if (!body)
|
|
|
body = new Body(f->GetBody());
|
|
@@ -65,10 +65,13 @@ Fixture::Fixture(b2Fixture *f)
|
|
|
|
|
|
Fixture::~Fixture()
|
|
|
{
|
|
|
- if (data != nullptr)
|
|
|
- delete data->ref;
|
|
|
+ if (!udata)
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (udata->ref)
|
|
|
+ delete udata->ref;
|
|
|
|
|
|
- delete data;
|
|
|
+ delete udata;
|
|
|
}
|
|
|
|
|
|
Shape::Type Fixture::getType() const
|
|
@@ -239,16 +242,24 @@ int Fixture::setUserData(lua_State *L)
|
|
|
{
|
|
|
love::luax_assert_argc(L, 1, 1);
|
|
|
|
|
|
- delete data->ref;
|
|
|
- data->ref = new Reference(L);
|
|
|
+ if (udata == nullptr)
|
|
|
+ {
|
|
|
+ udata = new fixtureudata();
|
|
|
+ fixture->SetUserData((void *) udata);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!udata->ref)
|
|
|
+ udata->ref = new Reference();
|
|
|
+
|
|
|
+ udata->ref->ref(L);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int Fixture::getUserData(lua_State *L)
|
|
|
{
|
|
|
- if (data->ref != nullptr)
|
|
|
- data->ref->push(L);
|
|
|
+ if (udata->ref != nullptr)
|
|
|
+ udata->ref->push(L);
|
|
|
else
|
|
|
lua_pushnil(L);
|
|
|
|
|
@@ -321,6 +332,10 @@ void Fixture::destroy(bool implicit)
|
|
|
Memoizer::remove(fixture);
|
|
|
fixture = nullptr;
|
|
|
|
|
|
+ // Remove userdata reference to avoid it sticking around after GC
|
|
|
+ if (udata && udata->ref)
|
|
|
+ udata->ref->unref();
|
|
|
+
|
|
|
// Box2D fixture destroyed. Release its reference to the love Fixture.
|
|
|
this->release();
|
|
|
}
|