lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thursday, November 15, 2012 07:15:35 AM Anthony S wrote:
> Pay attention to the commented out lua_pop() calls. If I call lua_pop()
> every time when value on stack is not needed anymore, test program works
> fine, without segfault. But my program is organized in such way that stack
> is not cleaned immediately. Lua calls are wrapped in C++ class, and stack
> is cleaned when the object is destroyed. IMHO this is still semantically
> correct, at the moment of segfault stack top is 65 and max stack limit
> 100000 is not reached yet, so the Lua has the bug

From the manual:

    When you interact with the Lua API, you are responsible for ensuring
    consistency. In particular, __you are responsible for controlling stack
    overflow.__ You can use the function lua_checkstack to ensure that the
    stack has extra slots when pushing new elements.

    Whenever Lua calls C, it ensures that the stack has at least LUA_MINSTACK
    extra slots. LUA_MINSTACK is defined as 20, so that usually you do not
    have to worry about stack space unless your code has loops pushing
    elements onto the stack.

You have a loop pushing elements onto the stack and no calls to 
lua_checkstack. Adding `lua_checkstack(lstate, 4);` at the start of each 
iteration should do it. (Unless I counted wrong.)

-- 
tom <telliamed@whoopdedo.org>