Search code examples
c++referencegarbage-collectionlualuabind

Problem with luabind::object dereferencing (simplified)


Using C++, lua5.1, luabind 0.7

Lua code:

-- allocates near 8Mb of memory
function fff()
    local t = {}
    for i = 1, 300000 do
        table.insert(t, i)
    end
    return t
end

C++ code:

{
    luaL_dostring(lua_state, "return fff()");
    luabind::object obj(luabind::from_stack(ls, -1));
}
lua_gc(l_, LUA_GCCOLLECT, 0); // collect garbage

Result: Lua still have a 8Mb allocated memory. Garbage collection ignores that table object. It has references anywhere? But where? That table deallocates only on program exit (when "lua_close" function called). How to solve this problem?

Thank you.


Solution

  • If the code you use is exactly as posted, I'd say there's still a reference in the Lua stack. Try to insert a lua_pop(l, 1) between the luabind::object creation and the lua_gc call.

    On a side note, the current stable release of luabind is 0.8.1, there's 0.9-rc also; you might get more answers if you were using some current version (both here and from the luabind-users group)