Re: [gtk-list] Pixmap freed




Didimo Emilio Grimaldo Tunon <emilio_tunon@nl.compuware.com> writes:

> What about the policy on releasing pixmaps from 'captivity'? say if I
> have an info/error/warning dialog that can pop up many times in the
> course of the application. I am getting the idea that by just creating
> a new pixmap and then not freeing it at the end of the dialog call I
> will be creating memory leaks :( so I suppose it is better to use
> a static variable to allocate the pixmap the first time it gets called
> and use it every other time as well. Can somebody correct me on this?

Well, you can do it either way. Keeping it in a static variable
will be a bit faster than creating/destroying it every time.

But the latter can certainly be done without leaking memory;
consider the reference counts for a pixmap:

 pixmap = gdk_pixmap_create_from_xpm (...) /* Refcount = 1 */
 widget = gtk_pixmap_new (pixmap, NULL);   /* Refcount = 2 */

 /* we unreference it here, since we don't need it any more */
 gdk_pixmap_unref (pixmap);                /* Refcount = 1 */

 [...]
 
 /* The widget gets destroyed - probably through destroying the
  * toplevel, not directly.
  */
 gtk_widget_destroy (widget);             /* Refcount = 0 */

When the pixmap's refcount drops to zero, the pixmap is destroyed.
So, just remember to unref() the pixmap when you are done using
it, and everything will be OK.

Regards,
                                   Owen

 



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