lua-users home
lua-l archive

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


Jamie Webb wrote:
I agree that __gc metamethods for tables would be very useful, but
there are complications: the reason AIUI that tables do not have them
is because a destructor might create new references to an object.
Unless Lua code can somehow be prevented from setting __gc (it's not
sufficient to require that it be a C function), probably the only good
solution is to move to a two-stage destruction, where the object has
to be shown unreachable again after the destructor has been called.
This is how Java does it.

__gc metamethods for tables are fairly easily fakeable -- I do it
by using a mapping table to associate weak-refs of the table to
a heavy userdata with the appropriate gc method attached to it.

I'm fuzzy on whether this indirectly allows resurrection of the
original table; to date I don't think I've had to worry about that.
IIRC I've asked on this list in the past whether it was safe for
a destructor to create new references, and was told that was fine.

__gc on a table is useful for the same reasons that one might
unify (heavy) userdata and tables, I think.  I use it so that I
can have the flexibility of a table attached to a C-side light
userdata and still have my C-side objects gc'd.

--Adam
--
Adam D. Moss   . ,,^^   adam@gimp.org   http://www.foxbox.org/   co:3