views:

173

answers:

1

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.

+2  A: 

See http://stackoverflow.com/questions/1939864/storing-a-lua-class-with-parent-in-luabindobject-updated. 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')
Daniel Wallin
Thank you very much. Specially for solution with "super = nil", because I can't compile luabind0.9rc to test this fix.
kFk