lua-users home
lua-l archive

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

On Wed, Dec 14, 2011 at 1:15 PM, Peter Pimley <> wrote:
Don't reduce your stack "periodically", pop all results after every
lua_call / lua_pcall, after copying them to somewhere else if you need
to keep their values.

I also tried this but it resulted in the same stack corruption =/

If you're in C++, it might be useful to have a RAII-style stack based
object that, in its destructor, asserts that the stack is in the
correct state.  Something like the following untested code:

struct StackCheck
 StackCheck (lua_State * lua) : lua(lua), top (lua_gettop(lua)) {}
 ~StackCheck () { if (lua_gettop(lua) != top) /* assert, throw
exception, whatever */ ;}
 const int top;
 lua_State * const lua;

// and then later on...
 StackCheck s (lua);
 // push function, arguments
 lua_call(lua, ......);
 // examine results
 // pop all results
} // s's destructor checks for stack overflow / underflow.

An exercise for the reader is to adapt StackCheck to have another
constructor argument, which is how many *extra* values are expected to
be on the stack when the destructor runs.

Debug output (prints of the stack content) immediately before the crashing calls seem to show the correct values in the correct places and I still get access violations.

As a side note, almost all calls to lua call the same function with minor changes in parameters (lua functions are called within for-loops).