C ++和Lua5.1-2 / LuaJIT

时间:2014-09-20 20:39:57

标签: c++ lua crash luajit

所以我正在使用LuaJIT在C ++应用程序中嵌入Lua5.1-2,并且包含了lua脚本能够“订阅”C ++代码中引发的事件并使用luaL_ref成功存储函数引用并成功调用的能力使用lua_rawgeti和lua_pcall的函数。

问题是在大约1500次调用该函数后,应用程序崩溃,没有抛出异常。出于好奇,我决定在调用lua函数之前和之后监视堆栈,并注意到lua_gettop开始返回递减的负值:('它'是迭代当前“订阅者”的迭代器,只有一个用于测试)

gameClient->appendToLog("Stack size before: " + std::to_string(lua_gettop(L));
lua_rawgeti(L, LUA_REGISTRYINDEX, it->second);
gameClient->appendToLog("Stack size [rawgeti]: " + std::to_string(lua_gettop(L));
int res = lua_pcall(L, 0, 0, 0);
gameClient->appendToLog("Stack size after: " + std::to_string(lua_gettop(L));

lua函数只包含用于测试的日志记录调用:

function update()
    log("Lua tick")
end

这是输出:

Stack size before: 0
Stack size [rawgeti]: 1
Lua tick
Stack size after: 0

Stack size before: 0
Stack size [rawgeti]: 1
Lua tick
Stack size after: 0

... repeats a few hundred times, then ..

Stack size before: 0
Stack size [rawgeti]: 1
Lua tick
Stack size after: -1

Stack size before: -1
Stack size [rawgeti]: 0
Lua tick
Stack size after: -1

... also repeats anywhere from 50-300 times before decreasing yet again

Stack size before: -1
Stack size [rawgeti] 0
Lua tick
Stack size after: -2

Stack size before: -2
Stack size [rawgeti]: -1
Lua tick
Stack size after: -2

这最终会导致应用程序崩溃。 lua函数没有返回值或参数,所以我没有想法。

注意:没有在lua函数中调用log(),我已经测试了这个。

编辑:在lua_pcall阻止崩溃后调用lua_settop(L,0),但我仍然想知道导致问题的原因,以便我可以修复它。

0 个答案:

没有答案
相关问题