Fix error when rapidly recreating objects;

If you create and destroy objects quickly (using :release), malloc
might give you the same pointer.  When we look up this pointer in
the userdata cache, it'll give you an invalid Proxy/pointer, which
throws an error like "Calling 'fn' on bad self".

When collecting objects, remove them from the userdata cache.
This commit is contained in:
bjorn 2021-06-26 14:41:42 -07:00
parent 4125796211
commit 63320252a1
1 changed files with 10 additions and 0 deletions

View File

@ -43,6 +43,16 @@ static int luax_meta__tostring(lua_State* L) {
static int luax_meta__gc(lua_State* L) {
Proxy* p = lua_touserdata(L, 1);
if (p) {
// Remove from userdata cache
lua_getfield(L, LUA_REGISTRYINDEX, "_lovrobjects");
if (lua_istable(L, -1)) {
lua_pushlightuserdata(L, p->object);
lua_pushnil(L);
lua_rawset(L, -3);
}
lua_pop(L, 1);
// Release
lua_getmetatable(L, 1);
lua_getfield(L, -1, "__info");
TypeInfo* info = lua_touserdata(L, -1);