Re: GObject and floating reference count



On Wednesday 31 May 2006 15:15, Tristan Van Berkom wrote:
Hannes Mayr wrote:
Hello,

I'm a little bit confused about the explanation of the floating
reference count.

The documentation says about GObject:
"The initial reference a GObject is created with is flagged as a
floating reference. This means that it is not specifically claimed to be
"owned" by any code portion."
http://developer.gnome.org/doc/API/2.0/gobject/gobject-The-Base-Object-Ty
pe.html#floating-ref


And about GtkObject:
"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;"
http://developer.gnome.org/doc/API/2.0/gtk/GtkObject.html

So, the part of GtkObject says GObject is owned by the creator and the
docs about GObject itself says it isn't owned by anyone (until
g_object_sink() is called).

Who is wrong here? Or maybe I unterstood something wrong...

This is a whole new can of bugs waiting to spill, the answer to your
question is that both are created floating in GTK+-2.10, before that
GObjects were never floating and floating widgets were the main incentive
to keep GtkObject around.


The documentation in Glib-2.10 is wrong.  Plain GObjects are still not created 
with a floating reference (if they were, existing code would be broken).  
Only GInitiallyUnowned objects (or objects sub-classed from 
GInitiallyUnowned) are created with a floating reference.  At present, the 
only objects in GDK/GTK+ deriving from GInitiallyUnowned are GtkObjects.  So 
existing code works as normal.  The inheritance tree is:

  GObject
     |
     |
GInitiallyUnowned 
     |
     |
 GtkObject

In effect the floating reference count code has been moved from GtkObject to 
GObject but floating references are only given to GIniatiallyUnowned objects; 
accordingly, with GTK+-2.9 the

  g_object_ref(G_OBJECT(obj);
  gtk_object_sink(GTK_OBJECT(obj));

sequence is deprecated (but will still work correctly) in favour of

  g_object_ref_sink(G_OBJECT(obj));

I posted a bug on this a few months ago and the documentation was corrected 
for Glib-2.11, but for some reason not for Glib-2.10.  I suggest someone 
posts another one to get the documentation corrected in Glib-2.10.

I have to say that moving the floating reference handling code to a class that 
does not in fact use it in its interface breaks all my training on writing 
object orientated code, and furthermore calling g_object_ref_sink() on a 
newly created plain GObject would be lgeal but positively wrong as the 
GObject would end up with a reference count of 2 and not 1 (because it 
started with a (non-floating) reference count of 1).  But no doubt the 
maintainers had their reasons (but they managed to confuse themselves 
sufficiently to get the documentation wrong).

Chris




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