mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Fix Issue #1273
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:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user