lua-users home
lua-l archive

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

Hi Roberto,

Am 2012-07-04 22:42, schrieb Roberto Ierusalimschy:
I guess the following structure does what you want.

Thank you very much!
However it requires ephemerons and thus Lua 5.2 to work, doesn't it?

I read when trying to understand what it does, which was very instructive, but I still don't understand how calling mksentinel(obj) again in the sentinel's __gc method can save the obj. Isn't it too late also for obj when the sentinel's __gc is run?

(Note that you cannot use the __gc metamethod of
'myobject', as it will be called once the object becomes gargage; you
must use 'collect' to finalize the object.)

So myobject's __gc metamethod is called every time the (sentinel, obj) pair is dropped from the anchor table? I understand that we thus have to do any finalization work in collect() instead, but I still wonder how mksentinel(obj) can save the obj when it seems that internally to Lua it was marked for collection at the time when the sentinel key in anchor became collectible.

Well, sorry that I didn't mention earlier that I'm still using Lua 5.1: we're not quite ready to upgrade to 5.2 yet (but I'll be among the first to purchase the PiL3 the day it comes out :-) ).

I assume there is no easy alternative of this code for Lua 5.1?

My current, simple but somewhat clumsy, work-around plan is to keep normal, strong references to my C++ objects by default, and once in a while (e.g once per frame, or every n-th frame, or whatever) iterate the strong references table, checking which of the objects still have references in C++ (smart pointer reference count > 1), and for those that don't, simply drop the entry from the strong references table. (And possibly re-insert it as required, e.g. whenever the object occurs in the stack of a CFunction again, the type checking mechanism would restore the strong reference...)

Best regards,

   Cafu - the open-source Game and Graphics Engine
for multiplayer, cross-platform, real-time 3D Action
          Learn more at