From 63320252a17f24f7bdc55894f345589b5a99bf4e Mon Sep 17 00:00:00 2001 From: bjorn Date: Sat, 26 Jun 2021 14:41:42 -0700 Subject: [PATCH] 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. --- src/api/api.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/api/api.c b/src/api/api.c index 88ab0f47..ecb1d574 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -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);