We have a problem in our project,we use lua 5.1 as our scripting language.
But when using lua_pushnumber to pass too many data from C++ to lua in one function,lua stack seems like stackoverflow and cause other part of the momery in C++ has been written,and it cause our system crash when the callback return to C++. I want to know whether there are some parameters to control the size of lua stack size.I try to change the parameter LUA_MINSTACK which defined in lua.h, but it seems doesn't work.I also try to use lua_checkstack() to avoid pushing too many number to lua stack but it also doesn't work
getNineScreenEntity(lua_State* L)
{
DWORD screenid = GET_LUA_VALUE(DWORD,1)
struct EntryCallback : public ScreenEntityCallBack
{
EntryCallback(){ }
bool exec(ScreenEntity * entity)
{
list.push_back(entity)
return true;
}
std::vector<ScreenEntity*> list;
};
EntryCallback exec;
Screen* screen = ScreenManager::getScreenByID(screenid);
if (!screen)
return 0;
screen->execAllOfScreenEntity(exec);
int size = 0;
std::vector<ScreenEntity*>::iterator vit = exec.list.begin();
for (; vit != exec.list.end(); ++vit)
{
lua_pushnumber(L,(*vit)->id);
++size;
}
return size;
}
It seems like when there are too many entities in one screen, our program will crash.
You should check Lua stack every time before pushing into it.
Stack size is 20 by default, it should be manually enlarged if more space required.
Lua manual
Maybe this will help (from Lua 5.2 manual)
"Ensures that there are at least 'extra' free stack slots in the stack. It returns false if it cannot fulfill the request, because it would cause the stack to be larger than a fixed maximum size (typically at least a few thousand elements) or because it cannot allocate memory for the new stack size. This function never shrinks the stack; if the stack is already larger than the new size, it is left unchanged."
Here is an example c function...
This code:
When the code above doesn't have the lua_checkstack() call, we get an error trying to push 1000 items onto the stack.
(1) stackDump() is similar to what appears in PiL 3rd ed. for dumping stack contents.