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



Tristan Van Berkom schreef:

Hans Oesterholt-Dijkema wrote:

L.S.,

I'm writing a language binding for mzscheme to Gtk. I recently
derived a method to do memory mangement for GtkObject and
GObjects. An artikle about it can be read here:

http://www.elemental-programming.org/mzgtk2-mzgtk2-memory-management.html

There is however one case, where I'm in trouble. See /"The containment assumption falsified"/
in this artikle. Now I have one burning question:

Is there a decision procedure to determine if some GtkObject or
GObject contains an other GObject? E.g., a GtkImage object will
contain a GdkPixmap. When asked for, with gtk_image_get_pixmap,
the reference count of the returned GdkPixmap will be 1. If my typemapper
can decide somehow (e.g. by questioning the GdkPixmap GObject) that
it is contained withing another object (the GtkImage GObject), my typemapper
can increase the reference count by 1.


    I'm not exactly clear as to why you must absolutely keep an
eye on every GObject created, note that a GtkImage which uses a
GdkPixbuf delagate is not really a "parent child" relationship.

The straight forward answer to your question is "no", you can
not find out what code segment is responsable for a given object,
GtkWidgets have a logical "heirarchy" which can be navigated
with the GtkWidget/GtkContainer APIs; if a GtkImage is finalized,
it is safe to say that it has released any references for any
external resources used.

Cheers,
                          -Tristan

What happens is this:

I create a GtkImage from file (i.e. GtkImage *owns* GdkPixbuf).

1. My binding uses "proxies" to all GObjects.
2. All proxies take ownership of all GObjects with ref count=1.
3. When the garbage collector finalizes a proxy (somewhere in the
   time, the reference count to the GObject is decreased.

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.

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.

Any solutions?

Thanks in advance for answers,

Hans





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