lua-users home
lua-l archive

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


>> If I move the invocation of __gc from GCTM to luaC_separateudata, I
>> get the desired behavior - but what implications does that have on the
>> collector?
>
> All finalizers will be called in the atomic phase, imparing the
> "incrementability" of the collector.

> But if you want someone to answer your questions, you should answer
> their questions as well. I had asked you why do you need/want to keep in
> your program a reference to an object that is going to die?

I keep the reference to maintain a bond between the user data and an underlying
object. The user data has a strong ref to the object and the object has a weak
reference to the user data. When the object's reference count is incremented, it
attempts to switch from a weak ref to a strong ref (to the user data).

To do so, it has to fetch the user data using its weak ref. The
problem is that that
weak ref may be gone - it may even point to something different. In
the user data's
__gc method, I divorce the underlying object from the user data. The object may
continue to exist on its own, but it does not have a proxy in Lua.

Here is the scenario more concretely:

User data is created.
  It creates a GObject and takes a strong ref on it.
    This ref has a notification mechanism that lets me know when it
becomes the last
    one or not the last one (toggle ref).
  The user data creates a weak ref to itself in a table and gives that
to the GObject.

<dip into event loop>

The collector sees that the user data can be collected and removes the
weak ref from
its table.
The user data's __gc metamethod is not called yet.
  At this point, the GObject has an invalid reference to the user data
- but no one knows
  about it.

<dip into event loop>

A new user data is created and the same thing happens, but this new,
unrelated user data
re-uses the weak ref index that the first user data had.
  At this point, the GObject has a reference to something it knows
nothing about.

<dip into event loop>

An event arrives for the GObject and it wants to deliver it to its user data.
  It uses the weak ref to fetch the user data.
    It gets the wrong user data and bad things happen when it tries to
deliver the event.


If I had a chance to know that the weak ref went away, I could let the
GObject know,
and separate it from its user data.


Thanks,
Pablo

(Sorry about the funky replies, but I cannot get the mailing list to send
me individual e-mails - it insists on sending me only the digests)