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:
- in luabind 0.7 - same result as without adopt policy
- in luabind 0.81 - crash with message "you are trying to use an unregistrerd type"
How can I correctly create a lua object in C++ and take it's ownership?