Re: GTK 1.2 - gtk_object_unref() trouble

on 9/27/00 7:07 AM, Martin Baulig at martin home-of-linux org wrote:

> Isn't gtk_object_destroy() supposed to set the refcount to
> zero thus making `object->ref_count > 0' always fail if the
> object got correctly destroyed or do I miss something ?

You are missing something. Calling gtk_object_destroy has no effect on the
ref. count. A destroyed object can have any ref. count and is not finalized
until the last ref goes away.

> Btw. I found this while I was wondering why
>     plug = bonobo_plug_new (<whatever>);
>     gtk_widget_destroy (plug);
> works without problems but
>     plug = bonobo_plug_new (<whatever>);
>     gtk_object_unref (plug);
> yields
>     Gtk-CRITICAL **: file gtkobject.c: line 1179 (gtk_object_unref):
>     assertion `object->ref_count > 0' failed.

That's because top level containers like GtkWindow and GtkWindow subclasses
are owned by the top level container list. They unref themselves when they
are destroyed. So you can't unref a BonoboPlug unless you ref it first,
because you aren't holding a ref (the ref is held by the "top level
container list"). But you can destroy a BonoboPlug and if there are no other
references the top level container list will then unref it so it will be
finalized as well as destroyed.

It's normally not sufficient to do gtk_object_destroy to get rid of an
object, because it has no effect on the ref. count -- gtk_object_destroy is
often a sign of a storage leak if no other code does an unref. But the
GtkWindow family is an exception to this rule. The correct way to get rid of
a GtkWindow or subclass is gtk_object_destroy.

I think this is confusing, but it's how things work in gtk.

    -- Darin

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