c++scriptinggarbage-collectionlualuabind

Storing a lua class with parent in luabind::object


Using C++, lua 5.1, luabind 0.7-0.81

Trying to create a lua class with parent and store it in a luabind::object.

Lua

class 'TestClassParent'
function TestClassParent:__init()
    print('parent init\n')
end
function TestClassParent:__finalize()
    print('parent finalize\n')
end

class 'TestClass' (TestClassParent)
function TestClass:__init()
    print('init\n')
    TestClassParent.__init(self)
end
function TestClass:__finalize()
    print('finalize\n')
end

C++

{
    luabind::object obj = luabind::call_function<luabind::object>(lua_state, "TestClass");
}
printf("before GC\n");
lua_gc(lua, LUA_GCCOLLECT, 0);
printf("after GC\n");

Output:
init
parent init
before GC
after GC

Result: After obj is destroyed, 'TestClass' instance is still alive after garbage collection cycle (__finalize method is not called and memory is not freed). It's destroying only on program exit.
Moresome if I use class without parent, garbage is collected correctly.

If I try to use adopt policy (to take ownership of created object)

luabind::object obj = luabind::call_function<luabind::object>(lua_state, "TestClass")[luabind::adopt(luabind::result)];

I get:

How can I correctly create a lua object in C++ and take it's ownership?


Solution

  • This is a known bug in 0.8.1; a reference to the last constructed object is left in the "super" function upvalue. It has been fixed in 0.9-rc1:

    http://github.com/luabind/luabind/commit/2c99f0475afea7c282c2e432499fd22aa17744e3