Luabind类派生问题(内存'泄漏') [英] Luabind class deriving problem (memory 'leak')

查看:107
本文介绍了Luabind类派生问题(内存'泄漏')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 luabind 0.81

简单测试来说明问题:

1)

  class'A'
函数A:__ init()
print('a init \ n')
end
函数A:__ finalize()
print('A finalize \\\
')
end

do
local obj = A()
end
collectgarbage(collect)

输出:

初始化

完成

2)

  class'A'
函数A:__ init()
print('a init \ n')
end
函数A:__ finalize()
print('A finalize \\\
')
end

class'B'(A)
函数B:__ init()
A .__ init(self)
print('B init\\\
')
end
函数B:__ finalize()
print('B finalize \\\
')
end
$ b $ do
local obj = B()
结束
collectgarbage('collect ')

输出:

init <
B init

问题:在垃圾回收时,父类不会被删除。


如何解决这个问题?谢谢。

解决方案

请参阅在luabind :: object中存储父类的lua类。这是同样的问题。 B 实例保留超级值。



设置全局 super 到 nil 在调用 collectgarbage()之前应该让问题消失:

  class'A'
函数A:__ init()
print('A init\\\
')
end
函数A:__ finalize()
print('A finalize \\\
')
end

class'B'(A)
函数B:__ init()
A .__ init(self)
print('B init \ n')
end
函数B:__ finalize()
print('B finalize \\\
')
end

do
local obj = B()
super = nil
end
collectgarbage('collect')


Using luabind 0.81

Simple test to illustrate the problem:

1)

class 'A'
function A:__init()
   print('A init\n')
end
function A:__finalize()
   print('A finalize\n')
end

do
   local obj = A()
end
collectgarbage("collect")

Output:
A init
A finalize

2)

class 'A'
function A:__init()
   print('A init\n')
end
function A:__finalize()
   print('A finalize\n')
end

class 'B' (A)
function B:__init()
   A.__init(self)
   print('B init\n')
end
function B:__finalize()
   print('B finalize\n')
end

do
   local obj = B()
end
collectgarbage('collect')

Output:
A init
B init

Problem: Class with parent is not deleted on garbage collection.

How to solve this problem? Thank you.

解决方案

See Storing a lua class with parent in luabind::object. This is the same problem. The B instance is left in a "super" upvalue.

Setting the global super to nil before calling collectgarbage() should make the problem go away:

class 'A'
function A:__init()
   print('A init\n')
end
function A:__finalize()
   print('A finalize\n')
end

class 'B' (A)
function B:__init()
   A.__init(self)
   print('B init\n')
end
function B:__finalize()
   print('B finalize\n')
end

do
   local obj = B()
   super = nil
end
collectgarbage('collect')

这篇关于Luabind类派生问题(内存'泄漏')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆