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
+26 -11
View File
@@ -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;
delete data;
if (udata->ref)
delete udata->ref;
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();
}