Storing a lua class with parent in luabind::object

谁说胖子不能爱 提交于 2019-12-20 02:45:27

问题


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?


回答1:


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




回答2:


Edit: after OP's update, this answer is no longer relevant, I'll leave it hanging here though. Daniel Wallin posted the correct answer

not really an answer, but I'd lose the formatting with a comment

I cannot reproduce this one. Here's the exact code I use:

// initialization
lua_State* lua = lua_open();
luaL_openlibs(lua);
luabind::open(lua);
// declare class
luaL_loadstring(lua, 
    "class 'TestClass'\
     function TestClass:__init() print('init') end\
     function TestClass:__finalize() print('finalize') end");
lua_pcall(lua, 0, LUA_MULTRET, 0);
// instantiate
{
    luabind::object obj = luabind::call_function<luabind::object>(lua, "TestClass");
}
// collect
printf("Before GC collect\n");
lua_gc(lua, LUA_GCCOLLECT, 0);
printf("After GC collect\n");
lua_close(lua);

And the result I get is:

init
Before GC collect
finalize
After GC collect

I'm using lua 5.1.4, luabind 0.81 with VC8 (aka VS2005) SP1




回答3:


Problem is not in luabind::object. It's in class derivation. But problem is not solved yet. Please see corresponding question - Luabind class deriving problem (memory 'leak')



来源:https://stackoverflow.com/questions/1939864/storing-a-lua-class-with-parent-in-luabindobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!