Re: GObject and Gtk+ Finalization



George <jirka 5z com> writes:
> > The following is OK with a floating object:
> >   Object *o = object_new ();
> >   pass_object_to_owner (o); /* does ref/sink */
> > 
> > with a nonfloating object, you have to do this:
> >   Object *o = object_new ();
> >   pass_object_to_owner (o); /* does ref */
> >   object_unref (o);
> >  
> > If this isn't what your code looks like, then your code was broken,
> > either the user code or the library.
> 
> The important thing is to make sure that the owner does not sink.  Not that
> the object is sunk.  You can do the second version even if sink is never
> called on the object.
>

If you simply ignore the floating reference, and do the second
version, then the object_unref (o) after passing to an owner is
invalid, because the caller of object_new() does not own a refcount. 

It is impossible for anyone to ever own the floating reference; that's
why it's floating. You can't remove it via unref(), only via sink().
If you want to own a reference, you must add one yourself. While with
a normally refcounted object (no floating reference), the caller of
object_new() implicitly owns the initial refcount of the object, and
therefore must either unref the object or pass its refcount to another
owner.

Any code that removes the floating reference with unref(), or assumes
that by leaving the floating reference in place the object won't
become invalid, is categorically broken.

Havoc




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