Added Lua index registry clear routine to `destroy` method of classes that use it.
If this is left to GC, the abandoned userdata would stay in memory for one full extra cycle.

Prettified the code slightly. Clarified some comments. Refactored `Fixture` class `udata` name.
Removed Reference class include from `Shape` (it doesn't use it).
This commit is contained in:
raidho36
2017-04-14 05:53:36 +03:00
parent bc8783869e
commit 6b7172bd40
9 changed files with 71 additions and 22 deletions
+20 -3
View File
@@ -61,8 +61,12 @@ Joint::Joint(Body *body1, Body *body2)
Joint::~Joint()
{
if (udata != nullptr)
if (!udata)
return;
if (udata->ref)
delete udata->ref;
delete udata;
}
@@ -175,6 +179,11 @@ void Joint::destroyJoint(bool implicit)
world->world->DestroyJoint(joint);
Memoizer::remove(joint);
joint = NULL;
// Remove userdata reference to avoid it sticking around after GC
if (udata && udata->ref)
udata->ref->unref();
// Release the reference of the Box2D joint.
this->release();
}
@@ -193,8 +202,16 @@ int Joint::setUserData(lua_State *L)
{
love::luax_assert_argc(L, 1, 1);
delete udata->ref;
udata->ref = new Reference(L);
if (udata == nullptr)
{
udata = new jointudata();
joint->SetUserData((void *) udata);
}
if(!udata->ref)
udata->ref = new Reference();
udata->ref->ref(L);
return 0;
}