Re: How to determine if a GObject is "contained"?



Hans Oesterholt-Dijkema wrote:
[...]
Normally when a GObject is "handed" to another GObject, the
reference count is increased, so, after taking ownership and handing over,
the GObject will have a reference count of 2.  Then, when the proxy to
the GObject is finalized, the reference count will drop to 1. No problems
here.

This isn't exactly true, passing ownership works like this:

 bar = my_bar_new();     // create an object
 foo_add_bar (foo, bar); // foo now "has bar in its inventory"
 g_object_unref (bar);   // give ownership of bar to foo.

ownership is the deciding reference count that will eventually
result in finalization if unreffed, all other references are
temporary.

GtkObject / GtkContainer is a special convenience case to avoid
the redundant g_object_unref () code (as stated above), note that
the initial gtk_container_add() call will not increse the reference
count on the child GtkObject (and *because* the reference is not
incremented ownership is passed).

However, not with the case of the GdkPixbuf of GtkImage. It returns
a GObject with reference count=1. mzgtk2 takes ownership of the GObject,
resulting in a situation that two Objects "think" they own the GObject.
When the proxy of mzgtk2 is finalized by the Garbage Collector, the
reference count of the GdkPixmap drops to 0, and it is prematurly
finalized.

When object 'foo' is created as a delagate of another object 'bar',
object foo "belongs to bar" and will be finalized by bar.

Why would a garbage collector be in any way interested in a resource
that was not explicitly allocated by the calling application code ?

I flipped through your previously referenced document... and
found this "Code sample 3", I really wonder what is the logic
behind this, is the goal of this excersize just to have a single
clean extra reference to every GObject ?
===========================================
 if (GTK_IS_OBJECT(g)) {
   if (GTK_OBJECT_FLOATING(g)) {
      g_object_ref(g);
      gtk_object_sink(g);
   }
   else {
      g_object_ref(g);
   }
 }
 else if (gobject_ref_count(g)>1) {
   g_object_ref(g);
 }
===========================================

May I ask why you aren't referencing objects that
have a ref_count of "1" and why you are not adding
a reference to floating GtkObjects ?

I obviously dont know enough about your project to solve
your problem, but I hope this was helpfull anyway.

Cheers,
                        -Tristan




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