lua-users home
lua-l archive

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


Tobias Käs wrote:
> Is it possible for userdata to tell the GC it has a (native)
> reference to another userdata object, and so prevent the GC to
> collect it?

Yes.  In Lua 4 you can use references (see lua_ref, lua_getref & lua_unref
in the manual.)  In Lua 5 you can use the registry table (at virtual index
LUA_REGISTRYINDEX) to store any private references that you need.  (The
reference calls are still available in 5.0, though they were removed from
the standard API and their usage is discouraged.)

You should also implement the "gc" tag method (Lua 4) or the "__gc" meta
method (Lua 5) for the userdata to release those object references (by
calling lua_unref or removing them from the registry) when the userdata is
about to be collected.

Bye,
Wim