[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: tables and the gc
- From: "Bilyk, Alex" <ABilyk@...>
- Date: Fri, 25 Apr 2003 20:03:54 -0700
This example doesn't cut it for me. It is often the case when one can not let Lua destroy objects in its own time due to system interdependencies. If system A needs system B to manipulate data members of A, clearly, by the time Lua decides to collect A, B may have long gone. So, there must be a way to GC things on the spot in a fairly straightforward and convenient fashion. What about weak keys and values? Could they be of any help to the original poster? I have never used them myself.
Alex
-----Original Message-----
From: Nick Trout [mailto:nick@rockstarvancouver.com]
Sent: Friday, April 25, 2003 6:37 PM
To: Multiple recipients of list
Subject: RE: tables and the gc
function create_resource() return "X" end
function delete_resource(r) end
class1 = {} -- simplification
function class1:create()
-- create hardware resource
self.res = create_resource()
return self
end
function class1:delete()
-- delete the resource
print("delete!")
self.res = delete_resource(self.res)
-- returning nothing returns nil
end
class2 = {} -- simplification
function class2:create()
self.class1 = class1:create()
print(self.class1.res)
end
function class2:free_hwr()
-- delete resource and set self.class1 to nil
self.class1 = class1:delete()
print(self.class1)
end
class2:create()
class2:free_hwr()
Gives you:
X
delete!
nil
I think this is what you want?
Using the GC to collect objects immediately will not be efficient.
Delete user objects explicitly and let the GC collect the Lua bits in
its own time.
You really don't want to go the way of forcing GC, especially as
incremental GC is on its way.
Mmmm. Friday. Sunshine. Beer. Have a good weekend and I hope I answered
your question.
Regards,
Nick
-----Original Message-----
From: Tom Spilman [mailto:tom@sickheadgames.com]
Sent: Friday, April 25, 2003 3:23 PM
To: Multiple recipients of list
Subject: tables and the gc
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