Re: Widget ref-count on creation



Tristan Van Berkom wrote:
Russell Shaw wrote:

so i assume this increases the refcount to 2 (couldn't tell
by looking at gtk source). So, if the containing window is
destroyed, it will release its refcount. However, the ttt
widget will still have a refcount of 1 and hang around.

What is the correct way of handling the refcount on widget
creation?


    I believe that at some point in time (gtk+-1.2 ==> gtk+-2.0 I think)
that this behaviour was changed, in the prior; the correct process was to:

// create widget
my_widget = g_object_new(MY_OBJECT_TYPE, NULL); // <-- refcount 1

// add to a container
gtk_container_add(container, my_widget); // <-- refcount 2

// pass ownership to the container
g_object_unref(my_widget);               // <-- refcount 1


But since then (because that code seemed redundant or encumbersome),
GtkObject and its subclasses are created with what is called a floating
reference, which in turn is unreferenced by the container implementation
by calling gtk_object_sink(); (after the container takes ownership of
the GtkObject by calling g_object_ref(); )

So any GtkObject at creation time has no "owner".

Ok, i think some clarifications are needed in the docs.

From: http://developer.gnome.org/doc/API/2.0/gtk/GtkObject.html

  The most interesting difference between GtkObject and GObject is the
  "floating" reference count. A GObject is created with a reference count
  of 1, owned by the creator of the GObject. (The owner of a reference is
  the code section that has the right to call g_object_unref() in order to
  remove that reference.) A GtkObject is created with a reference count of 1
  also, but it isn't owned by anyone; calling g_object_unref() on the
  newly-created GtkObject is incorrect. Instead, the initial reference count
  of a GtkObject is "floating".


This implies that GtkObject is created differently to GObject. However, in
gtkobject.c, gtk_object_new() simply creates a GObject.

So, the above explanation about floating references is really only a
matter of notation, because the floating reference is stored as a
"normal" reference.



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