Results 1 to 3 of 3

Thread: memory leak when copying Qimage data

  1. #1
    Join Date
    Feb 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default memory leak when copying Qimage data

    What is the best way (both in terms of time and of memory) to copy image data (to a Qlabel.pixmap or to another Qimage-object)?

    1. i have an object (let's call manipulator), that does some image manipulation. To copy the content of the image back i use this method:

    Qt Code:
    1. void manipulator::getPhaseMap(QImage *phasemap)
    2. {
    3. int w = _phasemap->width();
    4. int h = _phasemap->height();
    5. QRgb* pBits = (QRgb *) phasemap->bits();
    6. QRgb* _pBits = (QRgb *) _phasemap->bits();
    7.  
    8. for (int y=0;y<h;y++)
    9. {
    10. for (int x=0;x<w;x++)
    11. {
    12. pBits++;
    13. _pBits++;
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    I would have thought, that by copying the content of the pointers, no extra memory would be used. But actually each time this method is executed, it adds 4mb (the images are 1000x1000 pixels in size) to the application. Since i need to do that like once every second, this is a problem!

    (i recently found out, i could simply do

    *phasemap = *_phasemap;

    but i guess this takes a little bit longer, since it's not only the image data that is being copied..)

    2. Inside the manipulator-object i have the following method, that copies data back from another structure into the Qimage:

    Qt Code:
    1. void manipulator::buf2im(int w, int h, fftw_complex *f, QImage *im, int imag, float border)
    2. {
    3. float col = 0;
    4. double *bounds = new double[4];
    5. find_maxmin(w,h,f,bounds);
    6.  
    7. QRgb* pBits = (QRgb*) im->bits();
    8. for (int i=0;i<w;i++)
    9. {
    10. for (int j=0;j<h;j++)
    11. {
    12. if (imag == 1) col = 255 * (f[i*w+j][1] - border*bounds[2]) / (border*bounds[3] - border*bounds[2]);
    13. else col = 255 * (f[i*w+j][0] - border*bounds[0]) / (border*bounds[1] - border*bounds[0]);
    14. // *pBits = qRgb(col,col,col);
    15. *pBits = col;
    16. pBits++;
    17. }
    18. }
    19. delete bounds;
    20. }
    To copy to clipboard, switch view to plain text mode 

    When i use the line *pBits = col; i get 1mb of extra memory used. Using the *pBits = qRgb(col,col,col); i get 4mb! How am i supposed to change the image so that i don't get a memory leak?

    3. In a similar problem, i then try to show the Qimage inside a Qlabel. This is what i do:

    Qt Code:
    1. ui->phaseview->setPixmap(QPixmap::fromImage(_phaseimage.scaledToHeight(ui->phaseview->height())));
    To copy to clipboard, switch view to plain text mode 

    This also creates another 4mb of extra memory usage.

    Could someone point me to the best way to avoid those leaks?

    Thanks in advance. (i'm using Qt 4.6.2 on windows)

  2. #2
    Join Date
    Nov 2009
    Posts
    39
    Thanks
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: memory leak when copying Qimage data

    Are you sure the original image format is a 32 bit format?

  3. #3
    Join Date
    Feb 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak when copying Qimage data

    Quote Originally Posted by MSUdom5 View Post
    Are you sure the original image format is a 32 bit format?
    yes the first image is loaded from a jpg file and the second one is created with

    Qt Code:
    1. QImage( _image.size(), QImage::Format_RGB32);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QPixmap: X11 "memory leak" due to shared pixmap data?
    By chezifresh in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2009, 19:53
  2. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 08:02
  3. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31
  4. Memory leak
    By zlatko in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2006, 19:02
  5. Qt 4.1 Memory Leak
    By blackliteon in forum Qt Programming
    Replies: 14
    Last Post: 10th February 2006, 12:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.