lua-users home
lua-l archive

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


I recently had a bug which took me a very long time to track down, and
I'm still not entirely sure why an error occurs (though I fixed it).
Basically I had a "remove" method which removed an item from a
callback list (an array under the covers), but instead of resizing the
array then, I just set the value to "nil" and had a periodic cleanup
method which was called which got rid of all the nil values and
resized the array accordingly. This is how it looked:

while (lua_next(L, -2) != 0) {
 if (lua_isfunction(L, -1)) {
   funcPtr = lua_topointer(L, -1);

   if (funcPtr == searchPtr) {
     lua_pushvalue(L, -2);
     lua_pushnil(L);  // SEE HERE
     lua_settable(L, -5);

 lua_pop(L, 1);

Notice in the comment that I set the matching value to nil
mid-traversal. This works fine, most of the time, and this is what
made the bug tricky for me to track down. I simply changed that line
to the following:

lua_pushinteger(L, 0);

And the panics went away.

Now, I ask, should I not ever be setting a value to nil mid-traversal?
Why does this cause problems as I'm not modifying the key at all? My
initial assumptions were that some sort of GC was occuring before the
next iteration, which confused lua_next, since the value was GC'd
(nil). Is this correct?

Thank you,
Mitchell Hashimoto