我花了几个小时试着让它工作,但没有成功。我有一个C++程序与函数公开到Lua脚本。Lua脚本是这样使用它的:
setData('type', {a=1, b=2, c=3})所以第一个参数是string,第二个参数是table。
我当前最好的C++ setData function版本如下所示:
string sType;
map<string, uint8_t> mOptions;
int nArgs = lua_gettop(i_pState);
if (nArgs == 2)
{
if (lua_isstring(i_pState, 1) != 0)
sType = lua_tostring(i_pState, 1);
if (lua_istable(i_pState, 2) != 0)
{
lua_pushnil(i_pState);
while(lua_next(i_pState, 2))
{
uint8_t nValue = uint8_t(lua_tonumber(i_pState, -1));
string sKey = lua_tostring(i_pState, -2);
mOptions[sKey] = nValue;
lua_pop(i_pState, 1);
}
lua_pop(i_pState, 1);
}
}
return 0;它工作得很好,但在调用lua_close()函数后,在结束时会导致"program.exe已触发断点“错误。这段代码有什么问题?
UPD1。如果不处理第二个参数,我就不会得到错误。
UPD2。我的代码行为非常奇怪。我认为这可能是extern "C"和C++代码混合的问题。
UPD3。结果发现这是堆损坏。我不明白原因在哪里。这可能与lua一点关系都没有。
发布于 2018-11-04 02:27:11
终于来了!将Lua代码重新编译为C++代码不起作用(积极的一面是我不必再担心extern "C" )。但我发现了两个多余的lua_pop()调用。我删除了它们,现在它工作得很好。
我没想到Lua会这么危险!
https://stackoverflow.com/questions/53121268
复制相似问题