lua-users home
lua-l archive

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


On Fri, Oct 23, 2009 at 10:39 AM, corey johnson <probablycorey@gmail.com> wrote:
> I have a situation where Lua wants to free a userdata, but I need the
> userdata to stick around because separate obj-c code is still reliant
> upon the userdata. My workaround was to add new luaT_eventname that
> will return true if the obj-c code still needs the object (ie it's
> retain count > 1) .

Is the obj-c code reliant on the userdata because it wants to use it
as a lua object,
or just reliant on the userdata as C memory (perhaps there is a struct
in there)?

Assuming its just memory, and the obj-c code isn't goint to be making lua calls
on that userdata, then I would say the userdata should not be the
shared resource, it should have a
pointer to the shared resource. When lua collects the shared resource, decrement
its ref count. When an obj-c object with a ref to that resource gets released,
decrement a the ref count. Of course, making this shared resource a obj-c
object would give you something that already implements ref counts,
and retain/release.

Way easier than hacking the gc.

If you actually need your obj-c code to make lua calls on the
userdata, then you need a different
system, you need to create a lua table that maps obj-c objects to the
userdata they refer to (using
lightuserdata). When an obj-c object is released, it will clear the
mapping in the lua table. When lua
has no mappings, then it will collect the userdata.

Cheers,
Sam