[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: tables and the gc
- From: "Tom Spilman" <tom@...>
- Date: Fri, 25 Apr 2003 22:22:43 -0000
I have the following code (i'm using luabind, but this is a lua
issue )...
class 'my_class1'
function my_class1:__init()
self._my_hardware_resource = create_hardware_resource()
end
class 'my_class2'
function my_class2:__init()
self._my_class1 = my_class1()
end
function my_class2:free_hardware_resources()
self._my_class1 = nil
-- note that the class above is still not
-- released... must gc.
collectgarbage()
-- now 'my_class1' has been gc'ed, but
-- the internal member '_my_hardware_resource'
-- has not... must call gc again to do that.
collectgarbage()
end
Looking at my_class2.free_hardware_resources() you can see what my
problem is. It seems that the first garbage collection pass will
free a table ( the class members are in a table ), but will not free
newly unreferenced members of that table. A second garbage
collection pass is needed to do that. I assume that if i had a
deeper hierarchy i'd need to call the garbage collection call once
per level to ensure resources i have to have freed are.
Ok... stop the madness... what is the right way for me to free
something that must be destroyed immediately? I'm thinking
a "destructor" for 'my_class1' would be once solution as i could nil
the member before the gc call. Still calling collectgarbage() to
force the destruction of one object seems like overkill to me.
Should i just be using a different design pattern than what i've
used in other languages for years?
Tom