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:48 AM, Sam Roberts <> wrote:
> 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.

Thanks Sam,
It's a little more complicated than I let on. For a little background,
I've written an obj-c <-> lua bridge

The obj-c code actually calls Lua functions from userdata env, so I
need to use the userdata as the shared resource. I also currently have
a table with weak references that links the obj-c object to the
userdata, but don't have knowledge of when obj-c objects get released.
This is because I ask obj-c to create objects, I don't have knowledge
of when it is released. I also pass these objects off to other
methods, which then control the object's retain/release cycle.

For example...

local viewController = LuaViewController:init() -- returns userdata
containing an obj-c object and some lua functions i've added
self:navigationController():pushViewController_animated(viewController, true)

At this point Lua wants to clean up the viewController, but the Lua
methods in viewController could still be called from the

I'd love to avoid hacking the GC, but I keep running into walls when I
try solutions that avoid that. So know I'm just trying to figure out
how to get the current running thread from the Lua C API. But your
suggestions gave me some ideas. Thanks!