lua-users home
lua-l archive

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


On Wednesday 20, Francesco Abbate wrote:
> You will remark that the code is, from the logical point of view, the
> same of the code above with simple native Lua objects. What I was
> willing to do is to make the Lua GC do the work *normally*. The
> problem is that it can't because the GC is not able to determine the
> the userdata "b" (the plot) depends on the object "a" (the line,
> another userdata). This is why the people are using either:
> - reference counting
> - weak tables

and:
- userdata environment tables (these are not the same as weak tables)

> Clear enough ?

except you seem to be using reference counting of Line objects since you said 
before that multiple Plot objects can share the same Line objects.  Since the 
Line objects uses reference counting, you will need to use a boxed pointer 
for Line userdata objects.

If you try to use your custom 'new' allocator (the one that allocates your C++ 
objects inside a Lua userdata object) for the Line objects and have the Plot 
objects using reference counting of those Line objects, then there is going 
to be problems.  You shouldn't mix the two, since Lua will try to free the 
Line objects once the GC can't see any internal references.  The Lua GC can't 
look inside of the Plot objects even if the Plot objects are embedded inside 
the Lua userdata object.

If you really want to keep all your C++ objects allocated inside the userdata 
objects, then you will have to add an environment table to the Plot userdata 
object and add the Line userdata objects to that table when the 'add' method 
is called.  This will let the GC see which Line objects are referenced by a 
Plot object.

Use functions lua_setfenv()/luagetfenv() to set/get the environment table on a 
userdata object.

Personally I would use boxed pointers and reference counting for the Line 
objects (and any other object that is shared).

> Now, what I'm trying to say is: why we cannot add code to Lua core to
> make the GC aware that the userdata "b" depends (refers) to the
> userdata "a" so that the GC can make his job correctly without
> additional mechanisms ?

There is no need to change the Lua VM core.  Userdata objects already have 
environment tables so you can tell the GC about these types of dependencies.  
Other people has also said to use environment tables, have you tried this 
method?

-- 
Robert G. Jakabosky