lua-users home
lua-l archive

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


Hello,

It seems that the garbage collector (or whatever) removes nils from the
API stack even though the nils were already popped by hand. This leads
to lua_gettop giving negative values and then to memory error (which
was really hard to locate).

I have a code like this:

// BEGIN
lua_getglobal(L, name);

if(lua_isnil(L, -1)) // if name not found
{
	lua_pop(L,1); // get rid of the nil HERE THE TROUBLE COMES
	param = DEFAULT_param; // use a default value
} else
{
	param = (int) lua_tonumber(lua, -1); // read the value from lua
	lua_pop(lua,1); // get rid of the value from the stack
}
// END

The problem is that after the nil value is popped so that the stack is
empty and ready for another lua_getfield request, the GC removes one
element (perhaps GC thinks it is removing just the nil?) and the
lua_gettop gives -1 (in subsequent use it says -2 ...) and finally the
program crashes with either:

"double free or corruption"
or
"free(): invalid next size (fast)"

If I comment out the TROUBLED line, the nil is usually removed by GC
(or what), but not always!!! So the stack may grow (which is not
desired), but at least the program doesn't crash (so far).


It seems like a bug. Or if it were considered OK, I would expect that
users should be aware of this feature and were instructed not to remove
nil values from the stack themselves.

I use lua 5.1.4 on Fedora amd64.

Can anyone please comment on this?

Thank you,

Jan Šmydke