Re: [gtkmm] how to free a pixbuf



Hello!

On Wed, Oct 15, 2003 at 04:08:23AM -0400, Silviu D Minut wrote:
> I noticed that every time I advance to the next image, the memory usage
> increases by about the size of the image on disk, so I realized I have a
> memory leak. That is to be expected, because every time the pixbuf is
> created_from_file, new memory is allocated, but the old memory is not
> freed.
> 
> I saw Glib::RefPtr<some_type>::clear(), so I tried
> 
> pixbuf.clear();
> 
> just before I load a new image, but the memory still goes up as I read new
> images. What do I do?

The Gdk::Pixbuf uses the Glib::RefPtr, witch is a "reference counting
smart pointer". This means that you don't have to care about memory
management. If the RefPtr goes out of scope, the Pixbuf will be freed.

So you don't need to call Glib::RefPtr<some_type>::clear() or something
else. Consider this example (please emend me if I am wrong here):


/* This is untested. */
{
  /* Load the first pixbuf */
  Glib::RefPtr<Gdk::Pixbuf> mypixbuf = 
      Gdk::Pixbuf::create_from_file("myicon.png");
  /* Load the second pixbuf, the first one gets destroyed automatically */
  mypixbuf = Gdk::Pixbuf::create_from_file("my_new_icon.png");
} /* mypixbuf gets out of scope and will be destroyed */


Regards.

-- 
Simon Fuhrmann | NightSlayer at gmx.de | http://www.dismember.de
All people talk about the real life, but where can I download it?



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