Skip to content

Commit 6c5e337

Browse files
committed
Fixed a tricky crash that could sometimes occur if a temp luathread was garbage-collected early
1 parent e16aac9 commit 6c5e337

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Source/Lua/LuaAdapters.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "LuabindObjectWrapper.h"
55
#include "LuaMan.h"
66

7+
#include "lj_obj.h"
8+
79
using namespace RTE;
810

911
std::unordered_map<std::string, std::function<LuabindObjectWrapper*(Entity*, lua_State*)>> LuaAdaptersEntityCast::s_EntityToLuabindObjectCastFunctions = {};
@@ -295,11 +297,8 @@ void LuaAdaptersScene::CalculatePathAsync2(Scene* luaSelfObject, const luabind::
295297
// So, luabind::object is a weak reference, holding just a stack and a position in the stack
296298
// This means it's unsafe to store on the C++ side if we do basically anything with the lua state before using it
297299
// As such, we need to store this function somewhere safely within our Lua state for us to access later when we need it
298-
// It looks like Luabind constructs temporary interpreters and really doesn't like if you destroy these luabind objects
299-
// In any case, it's extremely unsafe to use! For example capturing the callback by value into the lambdas causes random crashes
300-
// Even if we did literally nothing with it except capture it into a no-op lambda
301-
// TODO: validate this is still actually the case. Changes to lua may have fixed this
302-
lua_State* luaState = callbackParam.interpreter();
300+
lua_State* luaState = mainthread(G(callbackParam.interpreter())); // Get the main thread for the state, in case we're a temp lua thread
301+
303302
static int currentCallbackId = 0;
304303
int thisCallbackId = currentCallbackId++;
305304
if (luabind::type(callbackParam) == LUA_TFUNCTION && callbackParam.is_valid()) {

0 commit comments

Comments
 (0)