luaW_gc uses stale reference count value and therefore doesn't call deallocator

Issue #6 resolved
qwer1304 NA
created an issue

Assume there's a single reference to an object. Therefore reference count equals 1. Assume Lua holds the object. Assume object isn't referenced anymore, so Lua calls the _gc metamethod. Assume _gc is bound to the default luaW_gc in LuaWrapper.hpp.

In LuaWrapper.hpp, in luaW_gc, 'count' is retrieved from the counts table, then the table is updated with (count - 1) and then there's a test whether (count == 0).

However, the value that is used in the test is PRIOR to the decrement, therefore the deallocator is not called.

Should add, e.g., count = count > 0 ? count-- : 0; after the line: int count = lua_tointeger(L, -1);

and change the line: lua_pushinteger(L, count-1);


lua_pushinteger(L, count);

PS The strange thing is that in the luawrapperexample project this is already fixed with: lua_pushinteger(L, --count);

But note that this is not safe as count could become negative.

Comments (5)

  1. Alex Ames repo owner

    Odd, I must have lost that correction in some refactoring I did at one point. There should be no fear of the count running negative as luaW_push and luaW_gc are the only places that adjust that value, and for every push there will be a corresponding gc eventually.

    This should be fixed in efa47b0cf3d6

  2. Log in to comment