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

编程入门 行业动态 更新时间:2024-10-28 18:31:26
本文介绍了在 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/luabind/luabind/commit/2c99f0475afea7c282c2e432499fd22aa17744e3" rel="noreferrer">http://github/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/luabind/luabind/commit/2c99f0475afea7c282c2e432499fd22aa17744e3

这篇关于在 luabind::object 中存储带有父级的 lua 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 03:32:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/747851.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:luabind   object   lua

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!