lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On May 20, 2014, at 5:15 PM, Eric Wing <ewmailing@gmail.com> wrote:

> 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.)

You cannot make assumptions about the order of GCing of unreferenced objects. Once object A is unreachable, then if the only references to B are via A (and hence B is also unreachable), both are GC candidates and can be collected in any order.

If you really need cleanup to occur in a defined order you will need to either release the reference to B first or make the __gc() metamethods co-ordinate the ordering of cleanup.

—Tim