lua-users home
lua-l archive

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

Hi Roberto,

Am 2012-07-05 18:30, schrieb Roberto Ierusalimschy:
No. 'obj' is still "alive" both as a upvalue for the sentinel's
finalizer and in the ephemeron. While a finalizer can access an object,
the object cannot be collected.  (However, if 'obj' had its own
finalizer, it would run in the first collection cycle after 'obj' became
not directly accessible.).

Many thanks for all the details!

I experimented a lot with your sample code, and I tried follow all its paths. However, right now I'm a bit more confused than less. As I'd really like to fully understand this, please let me ask again:

In the Lua 5.1 code, after

    myobject = nil

the myobject and the sentinel are referring to each other (sentinel is a value of myobject, and myobject is an upvalue of the sentinel's __gc metamethod), but the garbage collectors next "mark phase" would see them as an unreachable island and thus leave them both "white". (Or does that sentinel's upvalue reference cause one last time the myobject to become black?)

In the "cleaning phase", the sentinel with its __gc metamethod is put into the tmudata list. (If myobject had a __gc (in 5.2), it would be, too -- and gone for good.)
According to PiL2, chapter 31.2, the sentinel is also marked as alive / "black".
(I guess this is done to exclude it from the subsequent "sweep phase"?)

Is here the magic? Does the last step propagate from the userdata and thus also cause the referred myobject to become alive again?

The "sweep phase" by observation does not remove the myobject, and the "finalization phase" calls the __gc metamethod of the sentinel, creating a new sentinel for myobject for the next garbage collection cycle.

Where is my mistake?

Best regards,

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