[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: GtkImage/GdkPixbuf memory management
- From: Tristan Van Berkom <tvb gnome org>
- To: Michael Tweedale <m tweedale bristol ac uk>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: GtkImage/GdkPixbuf memory management
- Date: Wed, 06 Jun 2007 09:56:27 -0400
On Sun, 2007-06-03 at 16:56 +0100, Michael Tweedale wrote:
> Could someone clarify something that doesn't seem to be explicit in the
> API documentation?
>
> Consider the following code:
>
> static GtkWidget *image; /* initialized to a fixed GtkImage */
>
> void update(const char *s)
> {
> GdkPixbuf *pixbuf;
>
> pixbuf=gdk_pixbuf_new_from_file(s, NULL);
> if(pixbuf) {
> gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); /* <---| */
> g_object_unref(G_OBJECT(pixbuf));
> } else
> gtk_image_clear(GTK_IMAGE(image));
> }
>
> The question is: have I done enough to prevent a memory leak?
Yes.
> Suppose I call this function twice in succession:
> update("file1"); update("file2");
> When the indicated line executes the second time, is GtkImage smart
> enough to realize that it's replacing an existing image got from a
> GdkPixbuf, that when it _set_from_pixbuf that image it added a reference
> to the pixbuf, and that it should therefore now unref the pixbuf before
> replacing the image?
The cool part of a refcounted api here is that it doesnt matter
how GtkImage handles this, GtkImage could copy the pixbuf pixels
into a private pixmap or similar, or it could use the pixbuf
directly and hold a reference to it; in which case the previous
pixbuf would be released when updating the image for the second
time (or destroying the image), i.e. either:
- the pixbuf is just copied into an internal buffer and the pixbuf
freed when you call g_object_unref() (the api name kindof implies
this).
- ownership of the pixbuf is passed along to the image when you
call g_object_unref().
Cheers,
-Tristan
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]