lua-users home
lua-l archive

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

Michael Cumming wrote:
From what I understand from the archives and my own testing, there is no way for an object (table) to catch the GC? The reason for wanting to do this is for use with Luacom. Some COM objects need to have their Quit (or whatever) method called before you dispose of them. So is there anyway to catch the __gc without resorting to userdata?

There's a hacky and inconvenient way which does involve userdata, but
userdata that can be manufactured from a central place (i.e. your
tables gets to stay as tables without having to userdatify your
various object types individually).

In short, what you do is create a weak-valued table which maps
from dummy created-on-demant heavyuserdata to your {}s - the __gc
sits on the userdata and thus indirectly gets called when the {}
becomes collectable.  You then need a way to map back from the
userdata to the table, so the __gc func can find the table in
question.  (Details of that omitted because I can't understand my
own code for this, this late at night.)

It's pretty disgusting... I'd quite like a working __gc on
tables too. :)

Adam D. Moss   -