From 75f0892748d43961167c954bdeb2dd19a91f6eac Mon Sep 17 00:00:00 2001 From: maelstrom Date: Fri, 11 Jul 2025 01:39:18 +0200 Subject: [PATCH] fix(lua): re-added code that ensures that the thread (and therefore any upvalues) don't get gc'ed in signal handler --- core/src/datatypes/signal.cpp | 7 ++++++- core/src/datatypes/signal.h | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) 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: