Re: GObject and Gtk+ Finalization



On Sun, Sep 24, 2000 at 06:12:47PM -0400, Havoc Pennington wrote:
> > > 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. 

Not as far as I'm reading the code.  The only thing I see dealing with the
floating flag is the "sink" method.  And "sink" will unref the object
if the floating flag is set, and unset the floating flag.  The floating
reference count is still a normal reference count.

That is:

/* first version */
object_new
* floating: true, refcount: 1 */

  /* in pass_object_to_owner */
  _ref
  /* floating: true, refcount: 2 */
  _sink
  /* floating: false, refcount: 1 */

/* second version */
object_new
/* floating: true, refcount: 1 */

  /* in pass_object_to_owner */
  _ref
  /* floating: true, refcount: 2 */

/* back */
_unref
/* floating: true, refcount: 1 */

In both cases, after this we end up with a refcount of 1, which is
held by the pass_object_to_owner thing.  If you just ignore the
floating flag, things should just work.

> 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.

As far as I'm reading the code, the "floating reference" only means that
the object has a reference count of 1 and the FLOATING flag set.  unref
itself ignores the FLOATING flag.  I may be missing something.

Now of course, this assumes that no one ever calls _sink on this object.  But
they shouldn't and it would be a bug to do so then.

George

-- 
George <jirka 5z com>
   There is nothing so useless as doing efficiently that
   which should not be done at all.
                       -- Peter Drucker




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