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:
Is it possible for a weak ref to know when its peer object has been
deleted (and then assigned to null)?

Object is not assigned null. Variable is. Anyway, I believe it still is the
plan (and reason why the current kind is called 'unowned' rather than
'weak'), but is not done yet.

You might be actually able to set up your unowned references as weak using
the GObject.add_weak_pointer and GObject.remove_weak_pointer methods.
I should be even possible to get vala do it automatically with some clever
wrapper structure, though I am not sure all the needed features of generics
are in place.

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 a bit more complicated than that, because the object may go
away between your checking the reference is still valid and using it and
I don't actually think that one is the plan.

Something to better write the following code:
[...]
        Test **tr = &t;

That's a (strong) reference to a reference, not a weak reference. It defeats
any useful purpose of weak reference I can imagine. 

By the way, note that Java and C# don't have any weak references at all.
Unowned references in vala are mostly optimization to avoid some reference
coutning or copying that you have to be careful with and can do without. Full
weak references are obviously useless for that, since setting up the back
link is more expensive than the reference counting in the first place.

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



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