Skip to content

Commit cc1da08

Browse files
committed
fix(lua): re-added code that ensures that the thread (and therefore any upvalues) don't get gc'ed in signal handler
1 parent ebb1b4d commit cc1da08

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

core/src/datatypes/signal.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ LuaSignalConnection::LuaSignalConnection(lua_State* L, std::weak_ptr<Signal> par
2525

2626
// https://stackoverflow.com/a/31952046/16255372
2727

28-
// Save function so it doesn't get GC'd
28+
// Save function and current thread so they don't get GC'd
2929
function = luaL_ref(L, LUA_REGISTRYINDEX);
30+
lua_pushthread(L);
31+
// For posterity, since I accidentally removed this once, the parent thread must not be
32+
// deleted because it may (likely) contain upvalues that the handler references
33+
thread = luaL_ref(L, LUA_REGISTRYINDEX);
3034
}
3135

3236
LuaSignalConnection::~LuaSignalConnection() {
3337
// Remove LuaSignalConnectionthread so that it can get properly GC'd
3438
luaL_unref(state, LUA_REGISTRYINDEX, function);
39+
luaL_unref(state, LUA_REGISTRYINDEX, thread);
3540
}
3641

3742
#if 0

core/src/datatypes/signal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CSignalConnection : public SignalConnection {
4545

4646
class LuaSignalConnection : public SignalConnection {
4747
lua_State* state;
48-
int function;
48+
int function, thread;
4949

5050
friend Signal;
5151
protected:

0 commit comments

Comments
 (0)