Re: Can I use gtk_widget_unref() to releasetthe object created by gtk_invisible_new()?



(This is a bit better suited to gtk-app-devel-list, but...)

Brian Lu wrote:
Thanks a lot for your explanation. But look at following snippet from firefox latest code base:
    void InitWidget() {
        mWidget = gtk_invisible_new();
        gtk_object_ref(GTK_OBJECT(mWidget));
        gtk_object_sink(GTK_OBJECT(mWidget));
        gtk_widget_ensure_style(mWidget);
        mStyle = gtk_widget_get_style(mWidget);
    }
see http://lxr.mozilla.org/seamonkey/source/widget/src/gtk2/nsLookAndFeel.h#77
and
nsLookAndFeel::~nsLookAndFeel()
{
    //  gtk_widget_destroy(mWidget);
    gtk_widget_unref(mWidget);
}
see http://lxr.mozilla.org/seamonkey/source/widget/src/gtk2/nsLookAndFeel.cpp#78

Don't use Firefox as an example of how to best use gtk. They do some rather arcane things to implement their gtk backend that should never be done in normal gtk-only applications.

In most normal apps, you never need to _ref() or _unref() widgets. You just create them, add them to a container, and call gtk_widget_destroy() on the toplevel container when you want to get rid of them all.

You'd only use ref/unref if you want to keep widgets alive while reparenting them (that is, removing them from one container and then adding them to another).

Because of how gtk's memory management policy was designed, when you create a widget, you don't own a reference to it. There's a so-called 'floating reference' that no one owns. When you add a widget to a container, the container takes ownership of the widget and removes the floating reference. Calling _unref() yourself would be releasing a reference that isn't yours to release.

	-brian


I find mWidget is leaked (detected by using libumem.so provided by solaris).

I don't understand why calling gtk_object_ref() to increase mWidget reference count. This causes mWidget's reference count to be 2 and gtk_widget_unref() in deconstructor decreases it to 1.

Thanks

Brian



Markku Vire wrote:
Hi,

The caller doesn't own the returned reference in case of any gtk_*_new
functions, but the results are floating references (they are removed
once somebody calls gtk_object_sink). This is different from normal
GObjects. So, unreffing the returned value causes practically a double
free.

gtk_widget_destroy in turn runs dispose, ie. asks widget to drop
references to other widgets. Dropping external references usually causes
somebody else to drop the last reference to your widget, so it
efectively gets destroyed. In case of toplevels some global window list
is holding the reference.

Calling gtk_widget_destroy without taking ownership a a widget is
usually a bug that causes a memory leak, but this is not the case here,
since we are talking about a toplevel.

Cheers,

-Markku-
On Wed, 2008-01-09 at 17:39 +0800, Brian Lu wrote:
Hi, experts,

I found following codes will crash in gnome 2.21 environment:

  ...
  GtkWidget *foo = gtk_invisible_new();
  gtk_widget_unref(foo);
  ...

But it works well if gtk_widget_unref() is replaced with gtk_widget_destroy().

Does that mean that we can't use gtk_widget_unref() on such object and we can only
use gtk_widget_destroy() to release it?

Thanks

Brian
_______________________________________________
gtk-devel-list mailing list
gtk-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-devel-list

_______________________________________________
gtk-devel-list mailing list
gtk-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-devel-list



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