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

Storing a lua class with parent in luabind::object(在 luabind::object 中存储带有父级的 lua 类)

本文介绍了在 luabind::object 中存储带有父级的 lua 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C++lua 5.1luabind 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 类

基础教程推荐