简体   繁体   English

在luabind :: object中存储与父级的lua类

[英]Storing a lua class with parent in luabind::object

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

Trying to create a lua class with parent and store it in a luabind::object. 尝试使用parent创建lua类并将其存储在luabind :: object中。

Lua 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++ 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 在GC之前
after GC 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). 结果: obj被销毁后,'TestClass'实例在垃圾收集周期后仍然存活(未调用__finalize方法且未释放内存)。 It's destroying only on program exit. 它只在程序退出时销毁。
Moresome if I use class without parent, garbage is collected correctly. Moresome如果我使用类无父,垃圾收集正确。

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 luabind 0.7 - 与没有采取政策相同的结果
  • in luabind 0.81 - crash with message "you are trying to use an unregistrerd type" luabind 0.81 - 崩溃并显示消息“您正在尝试使用unregistrerd类型”

How can I correctly create a lua object in C++ and take it's ownership ? 如何在C ++中正确创建lua对象获取其所有权

This is a known bug in 0.8.1; 这是0.8.1中的已知错误; a reference to the last constructed object is left in the "super" function upvalue. 对最后构造的对象的引用保留在“超级”函数upvalue中。 It has been fixed in 0.9-rc1: 它已修复为0.9-rc1:

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

Edit : after OP's update, this answer is no longer relevant, I'll leave it hanging here though. 编辑 :OP更新后,这个答案已经不再适用了,我会把它留在这里。 Daniel Wallin posted the correct answer Daniel Wallin发布了正确答案

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 我正在使用lua 5.1.4,luabind 0.81和VC8(又名VS2005)SP1

Problem is not in luabind::object. 问题不在luabind :: object中。 It's in class derivation. 它是在类推导中。 But problem is not solved yet. 但问题还没有解决。 Please see corresponding question - Luabind class deriving problem (memory 'leak') 请看相应的问题 - Luabind类导出问题(内存'泄漏')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM