Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » GTK+
  • » How to determine if a GObject is "contained"? [RSS Feed]

#1 Nov. 15, 2005 18:49:55

Hans O.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to determine if a GObject is "contained"?


L.S.,I'm writing a language binding for mzscheme to Gtk. I recentlyderived a method to do memory mangement for GtkObject andGObjects. An artikle about it can be read here:http://www.elemental-programming.org/mzgtk2-mzgtk2-memory-management.htmlThere 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 orGObject contains an other GObject? E.g., a GtkImage object willcontain a GdkPixmap. When asked for, with gtk_image_get_pixmap,the reference count of the returned GdkPixmap will be 1. If my
typemappercan decide somehow (e.g. by questioning the GdkPixmap GObject) thatit is contained withing another object (the GtkImage GObject), my
typemappercan increase the reference count by 1.Any suggestions?Thanks in advance for your answers,Hans Oesterholt-Dijkema_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#2 Nov. 15, 2005 19:35:56

Tristan V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to determine if a GObject is "contained"?


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.htmlThere is however one case, where I'm in trouble. See /"The containmentassumption 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) thatit is contained withing another object (the GtkImage GObject), mytypemappercan 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

_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#3 Nov. 16, 2005 06:43:57

Hans O.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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.htmlThere is however one case, where I'm in trouble. See /"Thecontainment 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 mytypemappercan decide somehow (e.g. by questioning the GdkPixmap GObject) thatit is contained withing another object (the GtkImage GObject), mytypemappercan 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,
-TristanWhat 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


_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#4 Nov. 16, 2005 15:24:28

Tristan V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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

_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#5 Nov. 16, 2005 23:13:46

Hans O.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to determine if a GObject is "contained"?


Why would
a garbage collector be in any way interested in a resourcethat was not explicitly allocated by the calling application code ?It's not.My goal is to never have to use "g_object_unref" from
scheme.The GC can do that for me. However, I also do not want gobjects tobe prematurely destroyed.So what I do, as can be seen from the code sample 3, is, a GObjectproxy scheme object takes ownership of all GtkObjects and GObjectsthat it gets to see.You can see this overtaking of ownership in code sample 3. Thedecision procedure for taking over ownership for GtkObjects isobvious. When I floats, mzgtk2 takes ownership. When a GtkObjectis added to a container, the reference count will increase. Whenthe scheme object associated with it goes out of scope in scheme,the GC will clean up the proxy object, and while cleaning up, thereference count to the GtkObject is decreased. However, becausethe GtkObject had previously been added to a container, the containerwill be the last one (seen from the Gtk world) to have a referenceto the GtkObject.With GObjects it is more difficult. They are not floating. It isimpossible to detect, weather it has been created by some Gxyz_new()call, or as part of an other structure. When creater by some Gxyz_new()call, the code in code sample 3 does it's job right. By not increasingthe reference count of GObjects with ref_count 1, it takes ownershipof the GObject. However, this doesn't hold for GObjects thatare already owned by an other GObject,as is the case for a GtkImagewith a GdkPixbuf created from file.Now, what happens if I do nothing while accessing the GdkPixbuf ofa GtkImage (e.g., to set an icon with gtk_window_set_icon())?Because the ref. count of the GdkPixbuf = 1, mzgtk2 will take owenershipof it, by not increasing the reference count. Now the GdkPixbuf is ownedby the GtkImage and mzgtk2. And there it goes wrong. I hand the
GtkPixbufover to the gtk_window (using gtk_window_set_icon()). The referencecount to the GtkPixbuf is increased to 2 by that call. Next, the
GdkPixbufgoes out of scope in scheme. The reference count drops to 1. Next, theGtkImage (which was created within some scope and is no longer
necessary)goes out of scope, and the reference count drops to 0. What the..., nowthe GdkPixbuf is cleaned from memory, while still in use by the
GtkWindow.Ok, I need some decision procedure to determine if I get a GObject thatis already owned or if I get a *new* GObject.Still following? More to follow beneath the code sample.=========================================== 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 thathave a ref_count of "1" and why you are not addinga reference to floating GtkObjects ?So, that's to take ownership of these Objects, so that theGarbage Collector can do it's normal cleaning job.I obviously dont know enough about your project to solveyour problem, but I hope this was helpfull anyway.Best whishes and thanks,Hans Oesterholt-Dijkema.Cheers,                        -Tristan_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#6 Nov. 17, 2005 19:32:37

Joao V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to determine if a GObject is "contained"?


Hans, i haven't read all you wrote, but from what i read i think you
can solve the problem by using toggle_ref. It was a new API that was
added to GObject on the lastest release, exactly to solve binding
issues.

There was a long dicussion about this on java-gnome-hackers, and now
Java-Gnome uses that toggle_ref API. It also does some other tricks to
solve other memory management "problems".

Cheers,
J.V.


2005/11/16, Hans Oesterholt-Dijkema <>:
>
>
> Why would a garbage collector be in any way interested in a resource
> that was not explicitly allocated by the calling application code ?
> It's not. My goal is to never have to use "g_object_unref" from scheme.
> The GC can do that for me. However, I also do not want gobjects to
> be prematurely destroyed.
>
> So what I do, as can be seen from the code sample 3, is, a GObject
> proxy scheme object takes ownership of all GtkObjects and GObjects
> that it gets to see.
>
> You can see this overtaking of ownership in code sample 3. The
> decision procedure for taking over ownership for GtkObjects is
> obvious. When I floats, mzgtk2 takes ownership. When a GtkObject
> is added to a container, the reference count will increase. When
> the scheme object associated with it goes out of scope in scheme,
> the GC will clean up the proxy object, and while cleaning up, the
> reference count to the GtkObject is decreased. However, because
> the GtkObject had previously been added to a container, the container
> will be the last one (seen from the Gtk world) to have a reference
> to the GtkObject.
>
> With GObjects it is more difficult. They are not floating. It is
> impossible to detect, weather it has been created by some Gxyz_new()
> call, or as part of an other structure. When creater by some Gxyz_new()
> call, the code in code sample 3 does it's job right. By not increasing
> the reference count of GObjects with ref_count 1, it takes ownership
> of the GObject. However, this doesn't hold for GObjects that
> are already owned by an other GObject, as is the case for a GtkImage
> with a GdkPixbuf created from file.
>
> Now, what happens if I do nothing while accessing the GdkPixbuf of
> a GtkImage (e.g., to set an icon with gtk_window_set_icon())?
>
> Because the ref. count of the GdkPixbuf = 1, mzgtk2 will take owenership
> of it, by not increasing the reference count. Now the GdkPixbuf is owned
> by the GtkImage and mzgtk2. And there it goes wrong. I hand the GtkPixbuf
> over to the gtk_window (using gtk_window_set_icon()). The reference
> count to the GtkPixbuf is increased to 2 by that call. Next, the GdkPixbuf
> goes out of scope in scheme. The reference count drops to 1. Next, the
> GtkImage (which was created within some scope and is no longer necessary)
> goes out of scope, and the reference count drops to 0. What the..., now
> the GdkPixbuf is cleaned from memory, while still in use by the GtkWindow.
>
> Ok, I need some decision procedure to determine if I get a GObject that
> is already owned or if I get a *new* GObject.
>
> Still following? More to follow beneath the code sample.
>
>
>
>
> ===========================================
> 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 ?
> So, that's to take ownership of these Objects, so that the
> Garbage Collector can do it's normal cleaning job.
>
>
> I obviously dont know enough about your project to solve
> your problem, but I hope this was helpfull anyway.
> Best whishes and thanks,
>
> Hans Oesterholt-Dijkema.
>
>
>
> Cheers,
> -Tristan
>
>
>
> _______________________________________________
> gtk-list mailing list
> gtk-l***@*nome.org
>http://mail.gnome.org/mailman/listinfo/gtk-list>
>
>
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#7 Nov. 17, 2005 22:33:12

Hans O.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to determine if a GObject is "contained"?


I think it can help indeed, it is the support that is needed, but it is only
supported from gtk+ 2.8.x onwards. I'd like to support
earlier versions of Gtk+.

Do you have a link to the java-gnome-hackers discussion?

Thanks,
Hans


Joao Victor schreef:Hans, i haven't read all you wrote, but from what i read i think you
can solve the problem by using toggle_ref. It was a new API that was
added to GObject on the lastest release, exactly to solve binding
issues.

There was a long dicussion about this on java-gnome-hackers, and now
Java-Gnome uses that toggle_ref API. It also does some other tricks to
solve other memory management "problems".

Cheers,
J.V.


2005/11/16, Hans Oesterholt-Dijkema <>:Why would a garbage collector be in any way interested in a resource
that was not explicitly allocated by the calling application code ?
It's not. My goal is to never have to use "g_object_unref" from scheme.
The GC can do that for me. However, I also do not want gobjects to
be prematurely destroyed.

So what I do, as can be seen from the code sample 3, is, a GObject
proxy scheme object takes ownership of all GtkObjects and GObjects
that it gets to see.

You can see this overtaking of ownership in code sample 3. The
decision procedure for taking over ownership for GtkObjects is
obvious. When I floats, mzgtk2 takes ownership. When a GtkObject
is added to a container, the reference count will increase. When
the scheme object associated with it goes out of scope in scheme,
the GC will clean up the proxy object, and while cleaning up, the
reference count to the GtkObject is decreased. However, because
the GtkObject had previously been added to a container, the container
will be the last one (seen from the Gtk world) to have a reference
to the GtkObject.

With GObjects it is more difficult. They are not floating. It is
impossible to detect, weather it has been created by some Gxyz_new()
call, or as part of an other structure. When creater by some Gxyz_new()
call, the code in code sample 3 does it's job right. By not increasing
the reference count of GObjects with ref_count 1, it takes ownership
of the GObject. However, this doesn't hold for GObjects that
are already owned by an other GObject, as is the case for a GtkImage
with a GdkPixbuf created from file.

Now, what happens if I do nothing while accessing the GdkPixbuf of
a GtkImage (e.g., to set an icon with gtk_window_set_icon())?

Because the ref. count of the GdkPixbuf = 1, mzgtk2 will take owenership
of it, by not increasing the reference count. Now the GdkPixbuf is owned
by the GtkImage and mzgtk2. And there it goes wrong. I hand the GtkPixbuf
over to the gtk_window (using gtk_window_set_icon()). The reference
count to the GtkPixbuf is increased to 2 by that call. Next, the GdkPixbuf
goes out of scope in scheme. The reference count drops to 1. Next, the
GtkImage (which was created within some scope and is no longer necessary)
goes out of scope, and the reference count drops to 0. What the..., now
the GdkPixbuf is cleaned from memory, while still in use by the GtkWindow.

Ok, I need some decision procedure to determine if I get a GObject that
is already owned or if I get a *new* GObject.

Still following? More to follow beneath the code sample.




===========================================
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 ?
So, that's to take ownership of these Objects, so that the
Garbage Collector can do it's normal cleaning job.


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

Hans Oesterholt-Dijkema.



Cheers,
-Tristan



_______________________________________________
gtk-list mailing list
gtk-l***@*nome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list_______________________________________________
gtk-list mailing list
gtk-l***@*nome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#8 Nov. 17, 2005 22:39:13

Hans O.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to determine if a GObject is "contained"?


I've read some about this toggle_ref API. But will it really solve theissue aboutthe ownership of GObjects?

Joao Victor schreef:Hans, i haven't read all you wrote, but from what i read i think you
can solve the problem by using toggle_ref. It was a new API that was
added to GObject on the lastest release, exactly to solve binding
issues.

There was a long dicussion about this on java-gnome-hackers, and now
Java-Gnome uses that toggle_ref API. It also does some other tricks to
solve other memory management "problems".

Cheers,
J.V.


2005/11/16, Hans Oesterholt-Dijkema <>:Why would a garbage collector be in any way interested in a resource
that was not explicitly allocated by the calling application code ?
It's not. My goal is to never have to use "g_object_unref" from scheme.
The GC can do that for me. However, I also do not want gobjects to
be prematurely destroyed.

So what I do, as can be seen from the code sample 3, is, a GObject
proxy scheme object takes ownership of all GtkObjects and GObjects
that it gets to see.

You can see this overtaking of ownership in code sample 3. The
decision procedure for taking over ownership for GtkObjects is
obvious. When I floats, mzgtk2 takes ownership. When a GtkObject
is added to a container, the reference count will increase. When
the scheme object associated with it goes out of scope in scheme,
the GC will clean up the proxy object, and while cleaning up, the
reference count to the GtkObject is decreased. However, because
the GtkObject had previously been added to a container, the container
will be the last one (seen from the Gtk world) to have a reference
to the GtkObject.

With GObjects it is more difficult. They are not floating. It is
impossible to detect, weather it has been created by some Gxyz_new()
call, or as part of an other structure. When creater by some Gxyz_new()
call, the code in code sample 3 does it's job right. By not increasing
the reference count of GObjects with ref_count 1, it takes ownership
of the GObject. However, this doesn't hold for GObjects that
are already owned by an other GObject, as is the case for a GtkImage
with a GdkPixbuf created from file.

Now, what happens if I do nothing while accessing the GdkPixbuf of
a GtkImage (e.g., to set an icon with gtk_window_set_icon())?

Because the ref. count of the GdkPixbuf = 1, mzgtk2 will take owenership
of it, by not increasing the reference count. Now the GdkPixbuf is owned
by the GtkImage and mzgtk2. And there it goes wrong. I hand the GtkPixbuf
over to the gtk_window (using gtk_window_set_icon()). The reference
count to the GtkPixbuf is increased to 2 by that call. Next, the GdkPixbuf
goes out of scope in scheme. The reference count drops to 1. Next, the
GtkImage (which was created within some scope and is no longer necessary)
goes out of scope, and the reference count drops to 0. What the..., now
the GdkPixbuf is cleaned from memory, while still in use by the GtkWindow.

Ok, I need some decision procedure to determine if I get a GObject that
is already owned or if I get a *new* GObject.

Still following? More to follow beneath the code sample.




===========================================
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 ?
So, that's to take ownership of these Objects, so that the
Garbage Collector can do it's normal cleaning job.


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

Hans Oesterholt-Dijkema.



Cheers,
-Tristan



_______________________________________________
gtk-list mailing list
gtk-l***@*nome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list_______________________________________________
gtk-list mailing list
gtk-l***@*nome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

  • Root
  • » GTK+
  • » How to determine if a GObject is "contained"? [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support