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.
Maybe this will help (from Lua 5.2 manual)
int lua_checkstack (lua_State *L, int extra);
"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...
static int l_test1 (lua_State *L) {
int i;
printf("test1: on the way in"); stackDump(L);
int cnt = lua_tointeger(L, 1);
printf("push %d items onto stack\n", cnt);
printf("try to grow stack: %d\n", lua_checkstack(L, cnt));
for (i=0; i<cnt; i++) {
lua_pushinteger(L, i); /* loop -- push integer */
}
printf("test1: on the way out"); stackDump(L);
return 1;
}
This code:
- dumps the stack on the way into the function. (1)
- tries to expand the stack size to have 'cnt' free slots (it returns either true, it worked, or false, it didn't).
- pushes 'cnt' number of integers on the stack
- dumps the stack on the way out.
$ lua demo.lua
running stack test with 10 pushes
test1: on the way in
---1--
[1] 10
-----
push 10 items onto stack
test1: on the way out
---11--
[11] 9
[10] 8
[9] 7
[8] 6
[7] 5
[6] 4
[5] 3
[4] 2
[3] 1
[2] 0
[1] 10
-----
running stack test with 1000 pushes
test1: on the way in
---1--
[1] 1000
-----
push 1000 items onto stack
try to grow stack: 1
test1: on the way out
---1001--
[1001] 999
[1000] 998
[999] 997
[998] 996
[997] 995
[996] 994
...
When the code above doesn't have the lua_checkstack() call, we get an error trying to push 1000 items onto the stack.
running stack test with 1000 pushes
test1: on the way in
---1--
[1] 1000
-----
push 1000 items onto stack
Segmentation fault (core dumped)
$
(1) stackDump() is similar to what appears in PiL 3rd ed. for dumping stack contents.
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