Re: memory leaks - pixbuf
- From: Marcel Claro <marcelnmetal gmail com>
- To: Allen <allenfans gmail com>
- Cc: gtkmm-list gnome org
- Subject: Re: memory leaks - pixbuf
- Date: Mon, 25 May 2009 20:23:45 -0300
Thanks for help!
I don't think which i have a problem with "Image" type, because i use it as library and i did several test. But problems occurs in interface.
Below some of simplifyed code:
class ImageWindow{
Gtk::Window *imagewindow;
Gtk::Image *image;
//interface image
Glib::RefPtr<Gdk::Pixbuf> interfaceimage;
//Images
Image* workimage;
public:
//constructors
ImageWindow(string imagepath,MainWindow* main);
//One kind of image process
virtual void erosion();
}
ImageWindow::ImageWindow(string imagepath){
//interface load
Glib::RefPtr<Gnome::Glade::Xml> Widgetstree = Gnome::Glade::Xml::create("interface2.glade");
Widgetstree->get_widget("imagewindow", imagewindow);
Widgetstree->get_widget("image", image);
//Initializations
interfaceimage = Gdk::Pixbuf::create_from_file(imagepath);
workimage=pixbuftoImage(interfaceimage);
image->set(interfaceimage);
imagewindow->show_all();
}
void ImageWindow::erosion(){
Image *temp;
//Do image process
temp=MOperator::erosion(workimage,NULL...);
//image atualization
delete workimage;
workimage=temp;
image->clear();
interfaceimage.reset();
interfaceimage=imagetoPixbuf(workimage);
image->set(interfaceimage);
}
Image* pixbuftoImage(Glib::RefPtr<Gdk::Pixbuf> img){
Color_Image* out;
out = new Color_Image(img->get_height(),img->get_width());
ColorPixel **pixelmap = out->pixelmap;
unsigned char *pixel,*pixel_aux;
int bitesfactor,bitesperpixel;
pixel=img->get_pixels();
switch(img->get_bits_per_sample()){
case 8:
bitesfactor = 1;
bitesperpixel=bitesfactor*img->get_n_channels();
for(int i=0;i<img->get_height();i++)
for(int j=0;j<img->get_width();j++){
pixel_aux=pixel+i*img->get_rowstride() + bitesperpixel*j;
pixelmap[i][j]=ColorPixel(*pixel_aux,*(pixel_aux+1),*(pixel_aux+2));
}
break;
default:
//Error: Too much bits per pixel
break;
}
return out;
}
Glib::RefPtr<Gdk::Pixbuf> imagetoPixbuf(Image* img){
Glib::RefPtr<Gdk::Pixbuf> out;
unsigned char *data;
data="" unsigned char[img->getHeight()*(img->getWidth())*3];
for(unsigned int i=0;i<img->getHeight();i++)
for(unsigned int j=0;j<img->getWidth();j++)
for(unsigned int k=0;k<3;k++)
data[3*i*(img->getWidth())+3*j+k]=(unsigned char)(((float)256/img->getGreyLevels())*img->getPixel(j,i));
out = Gdk::Pixbuf::create_from_data((guint8*)data,Gdk::COLORSPACE_RGB,false,8,img->getWidth(),img->getHeight(),3*img->getWidth());
return out;
}
My program treats with images and image processing, and i think that manual memory management is better for me. Exist one way to do this, without RefPtr<>?
Thanks again!
2009/5/25 Allen
<allenfans gmail com>
Agree with Mark Roberts very much.
Dear Marcel!
> I have memory leaks with use of images...
>
> Glib::RefPtr<Gdk::Pixbuf> interfaceimage;
> Image* workimage;
> Gtk::Image *image;
> interfaceimage = Gdk::Pixbuf::create_from_file(imagepath);
> workimage=pixbuftoImage(interfaceimage);
> image->set(interfaceimage);
>
> Image is my type for images...
>
> image->clear();
> interfaceimage.reset();
> interfaceimage=imagetoPixbuf(workimage);
> image->set(interfaceimage);
It seems strange to call image->set() on an uninitialised pointer. This is
surely not how Gtk::Image::set(Glib::RefPtr<Gdk::Pixbuf>) is supposed to
be used.
If you do similar things inside your own type "Image", it is possible that
you manage to circumvent the automatic memory mangement provided by
RefPtr<>. How about posting a small but complete program which includes
your type "Image"? I'm sure we can spot a memory leak, if there is one.
All the best,
Mark
_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]