lua-users home
lua-l archive

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

Can't you just create a reference to A using luaL_ref in each object that needs a reference to it? When destroying these objects, luaL_unref the reference and A will be garbage collected when there are no references to it.



Francesco Abbate wrote:
2009/12/18 Sam Roberts <>:
On Fri, Dec 18, 2009 at 2:06 PM, Francesco Abbate <> wrote:
I was thinking that it would be nice if the userdata could store one
or more reference to other userdata objects in order to ensure that
the GC does not free the main object that own the resource if other
objects that need the same resource are still alive. In my idea the C
API could be something like:
Use lua_setfenv() to attach a table to your userdata.

In that table, put a reference to the userdata you want to not be available
for garbage collection while this new userdata exists.

Seems to be a nice solution! I was not aware that you can attach an
environment to a table. I think I'm going to stick with this solution
since it offers the same thing that I was suppose to achieve with
lua_setusedataref. I guess that in term of performance is still
reasonable... wait, is it really reasonable? to create a new
*environment* for each object ??? I guess yes since a new environment
it turns out to be just a Lua ordinary table.

In any case thank you very much for the help!