I am developing a program that uses Lua to create scripts, and sometimes it will crash. With GDB, I think I found the problem, but I don’t know if it solved it, since segfault will only happen sporadically. So the old code was as follows:
void Call(std::string func){ lua_getglobal(L, func.c_str()); //This is the line GDB mentioned in a backtrace if( lua_isfunction(L,lua_gettop(L)) ) { int err = lua_pcall(L, 0, 0,0 ); if(err != 0){ std::cout << "Lua error: " << luaL_checkstring(L, -1) << std::endl; } } }
The fact is that this function will be called several times per second, but the function to be called is not always defined, so I thought the stack would overflow. I added the following line:
lua_pop(L,lua_gettop(L));
And segfault is no longer happening. Could this be a problem?
source share