diff --git a/core/src/datatypes/signal.cpp b/core/src/datatypes/signal.cpp index 7c846f2..1ec0986 100644 --- a/core/src/datatypes/signal.cpp +++ b/core/src/datatypes/signal.cpp @@ -25,13 +25,18 @@ LuaSignalConnection::LuaSignalConnection(lua_State* L, std::weak_ptr par // https://stackoverflow.com/a/31952046/16255372 - // Save function so it doesn't get GC'd + // Save function and current thread so they don't get GC'd function = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushthread(L); + // For posterity, since I accidentally removed this once, the parent thread must not be + // deleted because it may (likely) contain upvalues that the handler references + thread = luaL_ref(L, LUA_REGISTRYINDEX); } LuaSignalConnection::~LuaSignalConnection() { // Remove LuaSignalConnectionthread so that it can get properly GC'd luaL_unref(state, LUA_REGISTRYINDEX, function); + luaL_unref(state, LUA_REGISTRYINDEX, thread); } #if 0 diff --git a/core/src/datatypes/signal.h b/core/src/datatypes/signal.h index 22b1062..db50950 100644 --- a/core/src/datatypes/signal.h +++ b/core/src/datatypes/signal.h @@ -45,7 +45,7 @@ public: class LuaSignalConnection : public SignalConnection { lua_State* state; - int function; + int function, thread; friend Signal; protected: