Memory problems with gdk_pixbuf

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Memory problems with gdk_pixbuf

John Coppens
Hello people.

A few weeks ago I posted a message about memory leaking. I haven't solved
the problem yet (thanks for a few privately received suggestions though).

The example below is taken largely from the GGAD manual, but, if the
segment is called repeatedly, allocates large chunks of memory, until a
certain point, where things break ('process killed')

--------------
global variable    GnomeCanvasItem *img_item = NULL;
--------------
  pxb = gdk_pixbuf_new_from_file(path, &err);

// Check if the image was previously allocated. If so, destroy it
// Note that this relies on the pxb being destroyed too. It seems
// this does _not_ happen (Valgrind indicate this)

  if (img_item)
    gtk_object_destroy(GTK_OBJECT(img_item));

  img_item = gnome_canvas_item_new(root,
                gnome_canvas_pixbuf_get_type(),
                "pixbuf", pxb,
                "x", 0.0, "y", 0.0,
                "width", pxb_w, "height", pxb_h,
                NULL);
  gdk_pixbuf_unref(pxb);
--------------
My question is, what is the mechanism to destroy canvas items so that all
resources are freed too. (The above is derived from example in the GGAD, I
believe). Do I have to free the pxb manually?


John
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Memory problems with gdk_pixbuf

Tristan Van Berkom
John Coppens wrote:

> Hello people.
>
> A few weeks ago I posted a message about memory leaking. I haven't solved
> the problem yet (thanks for a few privately received suggestions though).
>
> The example below is taken largely from the GGAD manual, but, if the
> segment is called repeatedly, allocates large chunks of memory, until a
> certain point, where things break ('process killed')
>
> --------------
> global variable    GnomeCanvasItem *img_item = NULL;
> --------------
>   pxb = gdk_pixbuf_new_from_file(path, &err);
>
> // Check if the image was previously allocated. If so, destroy it
> // Note that this relies on the pxb being destroyed too. It seems
> // this does _not_ happen (Valgrind indicate this)
>
>   if (img_item)
>     gtk_object_destroy(GTK_OBJECT(img_item));
>
>   img_item = gnome_canvas_item_new(root,
>                 gnome_canvas_pixbuf_get_type(),
>                 "pixbuf", pxb,
>                 "x", 0.0, "y", 0.0,
>                 "width", pxb_w, "height", pxb_h,
>                 NULL);
>   gdk_pixbuf_unref(pxb);
> --------------
> My question is, what is the mechanism to destroy canvas items so that all
> resources are freed too. (The above is derived from example in the GGAD, I
> believe). Do I have to free the pxb manually?

You could try:

(gdb) watch ((GObject *)pxb)->ref_count

and see if you can find a reference leek, maybe the
canvas item isn't releasing its reference when it gets
destroyed ? maybe there is a reference leek somewhere else ?

Cheers,
                       -Tristan


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Memory problems with gdk_pixbuf

John Coppens
On Mon, 06 Jun 2005 14:34:59 -0400
Tristan Van Berkom <[hidden email]> wrote:

> >
> >   img_item = gnome_canvas_item_new(root,
> >                 gnome_canvas_pixbuf_get_type(),
> >                 "pixbuf", pxb,
> >                 "x", 0.0, "y", 0.0,
> >                 "width", pxb_w, "height", pxb_h,
> >                 NULL);
> >   gdk_pixbuf_unref(pxb);

> You could try:
> (gdb) watch ((GObject *)pxb)->ref_count
>

Thanks Tristan.

Apparently, inside gnome_canvas_item_new, the ref_count gets incremented
three times (to 4), and decremented again to 2, which is fine. The unref
decrements again to 1, which is also correct.

The sentence:

     gtk_object_destroy(GTK_OBJECT(img_item));

Does not destroy the pxb item - the ref_count is not changed to 0... I
scanned over the gnomecanvas.c code, but I cannot find any code for
unreferencing the pixbuf... But then I'm not a great programmer and not
faniliar with the gnome insides.

Should I report this as a bug?

John
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Memory problems with gdk_pixbuf

Tristan Van Berkom
John Coppens wrote:

> On Mon, 06 Jun 2005 14:34:59 -0400
> Tristan Van Berkom <[hidden email]> wrote:
>
>
>>>  img_item = gnome_canvas_item_new(root,
>>>                gnome_canvas_pixbuf_get_type(),
>>>                "pixbuf", pxb,
>>>                "x", 0.0, "y", 0.0,
>>>                "width", pxb_w, "height", pxb_h,
>>>                NULL);
>>>  gdk_pixbuf_unref(pxb);
>
>
>>You could try:
>>(gdb) watch ((GObject *)pxb)->ref_count
>>
>
>
> Thanks Tristan.
>
> Apparently, inside gnome_canvas_item_new, the ref_count gets incremented
> three times (to 4), and decremented again to 2, which is fine. The unref
> decrements again to 1, which is also correct.
>
> The sentence:
>
>      gtk_object_destroy(GTK_OBJECT(img_item));
>
> Does not destroy the pxb item - the ref_count is not changed to 0... I
> scanned over the gnomecanvas.c code, but I cannot find any code for
> unreferencing the pixbuf... But then I'm not a great programmer and not
> faniliar with the gnome insides.
>
> Should I report this as a bug?

If the canvas item does `g_object_ref ()' in it's
set_property() method (or if setting the property results
in a reference increment), then the old pixbuf should be
unreffed when setting a new one & the current pixbuf
should be unreffed in the finalize or dispose method.

If the canvas item doesn't hold a reference to the pixbuf or
if it doesn't balance its references to the pixbuf,
then it is a bug, yes.

Cheers,
                                  -Tristan

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Memory problems with gdk_pixbuf

Maciej Katafiasz
In reply to this post by John Coppens
Dnia 06-06-2005, pon o godzinie 21:11 -0300, John Coppens napisał:
> The sentence:
>
>      gtk_object_destroy(GTK_OBJECT(img_item));
>
> Does not destroy the pxb item - the ref_count is not changed to 0... I
> scanned over the gnomecanvas.c code, but I cannot find any code for
> unreferencing the pixbuf... But then I'm not a great programmer and not
> faniliar with the gnome insides.

This is correct. Back in GGAD days, it would actually release the
object, however in GObject 2.0 things were changed, and destroy no
longer releases the object, merely calls its destructor. So if you have
any outstanding references, you need to unref them separately for object
to get freed.

Cheers,
Maciej

--
Maciej Katafiasz <[hidden email]>

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Memory problems with gdk_pixbuf

John Coppens
On Tue, 07 Jun 2005 17:13:16 +0200
Maciej Katafiasz <[hidden email]> wrote:

> Dnia 06-06-2005, pon o godzinie 21:11 -0300, John Coppens napisał:
> > The sentence:
> >
> >      gtk_object_destroy(GTK_OBJECT(img_item));
> >
> > Does not destroy the pxb item - the ref_count is not changed to 0... I
> > scanned over the gnomecanvas.c code, but I cannot find any code for
> > unreferencing the pixbuf... But then I'm not a great programmer and
> > not faniliar with the gnome insides.
>
> This is correct. Back in GGAD days, it would actually release the
> object, however in GObject 2.0 things were changed, and destroy no
> longer releases the object, merely calls its destructor. So if you have
> any outstanding references, you need to unref them separately for object
> to get freed.
>
Hi Maciej.

I'm confused. The GGAD (GTK+/Gnome Application Development) is the
"official GNOME programming manual" from the developer pages at Gnome. If
this manual is not updated, shouldn't there be a LARGE warning that is
doesn't apply to the 2.x series of things?

Is there some document that describes exactly those differences?

The 'GNOME Canvas Library Reference Manual' for gnome 2.2 describes how
canvasitems are made ('new') but not how they are destroyed, and what
happens to the objects they own.

Thanks,
John
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Memory problems with gdk_pixbuf

Maciej Katafiasz
Dnia 07-06-2005, wto o godzinie 13:22 -0300, John Coppens napisał:

> > > The sentence:
> > >
> > >      gtk_object_destroy(GTK_OBJECT(img_item));
> > >
> > > Does not destroy the pxb item - the ref_count is not changed to 0... I
> > > scanned over the gnomecanvas.c code, but I cannot find any code for
> > > unreferencing the pixbuf... But then I'm not a great programmer and
> > > not faniliar with the gnome insides.
> >
> > This is correct. Back in GGAD days, it would actually release the
> > object, however in GObject 2.0 things were changed, and destroy no
> > longer releases the object, merely calls its destructor. So if you have
> > any outstanding references, you need to unref them separately for object
> > to get freed.
> >
> I'm confused. The GGAD (GTK+/Gnome Application Development) is the
> "official GNOME programming manual" from the developer pages at Gnome. If
> this manual is not updated, shouldn't there be a LARGE warning that is
> doesn't apply to the 2.x series of things?

GGAD by Havoc Pennington describes GNOME 1.x platform and GTK+ 1.2.
While largely similar, certain fundamental things did change in 2.x, so
it's no longer relevant as it were once. Poor up-to-dateability of
developer pages is known bug, and various people are (or were) trying to
improve things. Filing a bug in bugzilla on this particular misleading
info is not a bad idea.

The successor to GGAD book is The Official GNOME 2 Developer's Guide by
Matthias Warkus, see http://www.nostarch.com/frameset.php?startat=gnome

> Is there some document that describes exactly those differences?

Various. Most fundamental changes in GLib/GTK+ platform for example are
covered by FAQ and migration section in API reference documentation.

> The 'GNOME Canvas Library Reference Manual' for gnome 2.2 describes how
> canvasitems are made ('new') but not how they are destroyed, and what
> happens to the objects they own.

Because it's not anything specific to Canvas. Canvas items are normal
GObjects, and as such, are subject to usual lifetime rules. The only
exception here is GtkObject -- subclasses of it behave differently as to
who owns initial reference, using so called "floating reference".
There's documentation on it available on net, although not as gathered
in one place as we could wish -- that's part of the ongoing effort to
improve developer pages I mentioned above.

Cheers

--
Maciej Katafiasz <[hidden email]>

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Memory problems with gdk_pixbuf

John Coppens
On Tue, 07 Jun 2005 21:58:43 +0200
Maciej Katafiasz <[hidden email]> wrote:

> The successor to GGAD book is The Official GNOME 2 Developer's Guide by
> Matthias Warkus, see http://www.nostarch.com/frameset.php?startat=gnome

Hi Maciej...

Thanks - that was very complete. I wonder if there is an electronic
version of the book yet? Ordering paper books from Argentina is a headache
(apart from costly).

I've got the leakage problem solved, it seems (though I really don't know
when it solved itself). Apparently the canvasitem _does_ release the pxb,
because when I tried to do it explicitly and this caused worse problems.

I traced a bit more, and found the ref_count to decrement to 0, and then
back up to 1 and back to 0, all inside the canvas item destruction.

Juggling with other instructions somehow solved the problem though.

Again, my thanks,

John
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list