Storing a lua class with parent in luabind::object(在 luabind::object 中存储带有父级的 lua 类)
问题描述
使用C++、lua 5.1、luabind 0.7-0.81
尝试创建一个带有父类的 lua 类并将其存储在 luabind::object 中.
Lua
class 'TestClassParent'函数 TestClassParent:__init()打印('父初始化
')结尾函数 TestClassParent:__finalize()打印('父完成
')结尾类TestClass"(TestClassParent)函数测试类:__init()打印('初始化
')TestClassParent.__init(self)结尾函数 TestClass:__finalize()打印('完成
')结尾C++
<代码>{luabind::object obj = luabind::call_function(lua_state, "TestClass");}printf("GC前
");lua_gc(lua, LUA_GCCOLLECT, 0);printf("GC后
"); 输出:
初始化
父初始化
GC前
GC后
结果: obj 被销毁后,'TestClass' 实例在垃圾回收周期后仍然活着(__finalize 方法未被调用,内存未被释放).它仅在程序退出时销毁.
Moresome 如果我在没有父级的情况下使用类,则会正确收集垃圾.
如果我尝试使用采用策略(获取创建对象的所有权)
luabind::object obj = luabind::call_function(lua_state, "TestClass")[luabind::adopt(luabind::result)]; 我明白了:
- 在 luabind 0.7 中 - 与不采用策略的结果相同
- 在 luabind 0.81 中 - 崩溃并显示消息您正在尝试使用未注册类型"
我怎样才能正确在 C++ 中创建一个 lua 对象并取得它的所有权?
这是 0.8.1 中的已知错误;对最后构造对象的引用保留在超级"函数 upvalue 中.它已在 0.9-rc1 中修复:
http://github.com/luabind/luabind/commit/2c99f0475afea7c282c2e432499fd22aa17744e3" rel="noreferrer">http://github.com/luabind/luabind/commit/2c99f0475c283c27fc9fd275243c77fd27524ac70/p>
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
')
end
function TestClassParent:__finalize()
print('parent finalize
')
end
class 'TestClass' (TestClassParent)
function TestClass:__init()
print('init
')
TestClassParent.__init(self)
end
function TestClass:__finalize()
print('finalize
')
end
C++
{
luabind::object obj = luabind::call_function<luabind::object>(lua_state, "TestClass");
}
printf("before GC
");
lua_gc(lua, LUA_GCCOLLECT, 0);
printf("after GC
");
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?
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
这篇关于在 luabind::object 中存储带有父级的 lua 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 luabind::object 中存储带有父级的 lua 类
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
