[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Confused by Lua garbage collection tear down order when there are references between objects
- From: Eric Wing <ewmailing@...>
- Date: Tue, 20 May 2014 17:15:58 -0700
I am trying to create a simple strong reference (Object A holds a
reference to Object B) such that:
1) Object A keeps Object B alive (if all other references are disposed)
2) B outlives A
I'm seeing #1, but #2 is not working.
I am using userdata and using set/getuservalue in Lua 5.2/5.3w2 and
basically putting objectB in objectA's user value table. (My specific
implementation has Object A keep an array of objects it keeps strong
references to.)
Is this supposed to work (thus I have a bug in my code), or is Lua's
GC breaking the reference early (thus I can't depend on the final tear
down order). If the latter, how might I address this?
(A more concrete example of why I need this is, imagine an OpenGL
texture (A) and the OpenAL context (B). The texture needs to keep the
context alive and a texture must be deleted before the context.)
This is my Lua psuedocode if it helps:
objectA = example.newObject()
do
local objectB = example.newObject()
example.AssociateReference(objectA, objectB)
objectB = nil
collectgarbage()
-- objectA is successfully keeping objectB alive
end
objectA = nil
collectgarbage()
-- I expect objectA needs to be collected first to release its
reference to objectB, but in testing I'm seeing objectB get collected
first
Thanks,
Eric
--
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/