C++ 执行lua c++;函数多次执行会导致lua脚本失败,且值为nil

C++ 执行lua c++;函数多次执行会导致lua脚本失败,且值为nil,c++,lua,C++,Lua,Lua脚本 algdata={lua_obj1=-1,lua_obj2=-1} return algdata c++代码 初始化 更新循环 int result; printstackDump(L); lua_newtable(L); int tableindex = 1; lua_pushnumber(L, tableindex++); lua_pushnumber(L, fdcur->var1); lua_rawset(L, -3); lua_pushnumber(L, tablei

Lua脚本

algdata={lua_obj1=-1,lua_obj2=-1}
return algdata
c++代码

初始化

更新循环

int result;
printstackDump(L);
lua_newtable(L);
int tableindex = 1;
lua_pushnumber(L, tableindex++);
lua_pushnumber(L, fdcur->var1);
lua_rawset(L, -3);
lua_pushnumber(L, tableindex++);
lua_pushnumber(L, fdcur->var2);
lua_rawset(L, -3);
lua_pushnumber(L, tableindex++);
lua_pushnumber(L, fdcur->var3);
lua_rawset(L, -3);

lua_setglobal(L, "data");
result = lua_pcall(L, 0, 1, 0);
if (result) {
    fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
}
ar->lua_obj1 = GetNumberFromLua(L, "lua_obj1");
ar->lua_obj2 = GetNumberFromLua(L, "lua_obj2");
ar->lua_obj3 = GetNumberFromLua(L, "lua_obj3");
printstackDump(L);
lua_pop(L, 1);
第一次更新: 第一个堆栈转储(第一行)=“功能” 第二个堆栈转储(最后一行-pop之前)=“表”

第二次更新: 第一次堆垛(第一行)=''(空)
lua_pcall失败=尝试调用nil值

lua_pcall已被替换为

int luaL_dofile (lua_State *L, const char *filename);
Loads and runs the given file. It is defined as the following macro:

     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
It returns 0 if there are no errors or 1 in case of errors.
如果需要,这将在内存中重新加载文件,否则调用pcall

Remove
lua_pop(L,1)来自更新循环
int luaL_dofile (lua_State *L, const char *filename);
Loads and runs the given file. It is defined as the following macro:

     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
It returns 0 if there are no errors or 1 in case of errors.