Re: [Vala] Does vala provide syntactic sugar for "weak" references that know when its peer has been nulled?



On Wed, Mar 03, 2010 at 15:22:59 -0600, Sandino Flores Moreno wrote:
Hello.
Is it possible for a weak ref to know when its peer object has been
deleted (and then assigned to null)?

Because, suppose you have a weak reference, then the real object is deleted.
So your "weak reference" should be notified, otherwise you might get segfaults.

It is actually supported by GLib.Object. Just create an unowned reference and
register it to the object via it's weak_ref method. It will get reset to null
when the object is finalized.

It will, however, not work in multi-threaded environment where one thread
could start destroying the object just as you read the pointer, so you can
still get non-null, but the object gets finalied while you use it, causing
a segmentation fault. Similar problem may happen if you do something with the
reference that might release the object and don't temporarily copy it to an
owned one.

It would be possible to implement a weak reference object, similar to how
it's done in java or python, that would not have these problems, but it is
a bit more complicated (a lock is needed etc.).

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



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