[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Userdata and the GC
- From: corey johnson <probablycorey@...>
- Date: Thu, 12 Nov 2009 14:07:58 -0800
On Fri, Oct 23, 2009 at 3:09 PM, Mark Hamburg <email@example.com> wrote:
> Create a userdata object that exists purely to get collected and have its
> __gc metamethod called. When the metamethod gets called, scan the strong set
> looking for objects with retain counts of 1 and remove them from the set.
> Then create a new object to get collected on the next GC cycle. Provided the
> number of objects is small, you can do the scan quickly.
I'm just getting around to implementing the code from Mark but am not
sure how this edge case would be handled.
-- Note: Lua Proxy Userdata is what holds the reference to the obj-c object
function dummyCode(array) -- array is an instance of an obj-c NSArray Object
local thing = NSObject:alloc():init() -- Assume the Lua Proxy
Userdata for 'thing' is automatically put into the strong set
... -- Misc Lua code. Assume the Lua garbage collector will be run
during this code. Since 'thing' is
-- not being retained by any other obj-c code (its retain
count is 1) it is removed from the strong set.
array:insertObject(thing) -- Now 'thing' has been retained by
obj-c (its retain count is 2), but Lua has no way of knowing
-- it should be added back
into the strong set.
thing = nil -- Now the Lua Proxy Userdata will be destroyed on the
next garbage collection, yet the 'thing' obj-c object is still
-- being used and will need the Lua Proxy Userdata
I may not have understood the implementation correctly. So any help
you could give would be greatly appreciated!