Results 1 to 6 of 6

Thread: QImage.scanLine pointer memory leak

  1. #1
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QImage.scanLine pointer memory leak

    Hi,

    I have a piece of code to display images one after the other. I noticed a memory leak when I "play" 100 images or so.

    Here is the code
    Qt Code:
    1. QImage image = QImage(width, height, QImage::Format_RGB32);
    2. uint * scanLine;
    3. for (int i = 0; i < height; i++)
    4. {
    5. scanLine = (uint *) image.scanLine(i);
    6. for (int j = 0; j < width; j++)
    7. {
    8. pixel = 0xff - c[i * width + j];
    9. scanLine[j] =qRgb(pixel, pixel, pixel); //memory leak here
    10. }
    11. }
    12. delete [] c;
    To copy to clipboard, switch view to plain text mode 

    The image is displayed correctly but I cannot figure out what I am doing wrong here and why there is a memory leak.
    height and width are 3000. c[] is a table of char with the image values. The image is a greyscale.

    Can anyone help me solving this memory leak?

    Cheers,
    Arnaud

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QImage.scanLine pointer memory leak

    The image should delete all of the memory it has allocated.

    Which leak checker did you use that says that the QImage leaks its memory?

    Cheers,
    _

  3. #3
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QImage.scanLine pointer memory leak

    Hi,

    I am developing on linux and I use the system monitor. it is pretty clear. Each time I run the above code in loop to display successively 100 images the is a leak of ~3.5GB. If I run the loop again an additional 3.5GB is leaked. When I shutdown the app all memory is freed.

    Commenting out scanLine[j] =qRgb(pixel, pixel, pixel); solve the problem. I'll try to reproduce with simple test code and try to find my error.

    Cheers,
    Rno

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QImage.scanLine pointer memory leak

    And the QImage itself goes out of scope after it has been used?

    Cheers,
    _

  5. #5
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: QImage.scanLine pointer memory leak [SOLVED]

    Hi,

    Yes the image is out of scope but you guided me toward the solution by simplifying the code.

    The problem is not the code above but below (I should have included it)
    Qt Code:
    1. m_scene->addPixmap(QPixmap::fromImage(GetImageFromDetector(ExpData.FileAndPath)))
    To copy to clipboard, switch view to plain text mode 

    I am adding a Pixmap to Qgraphicscene rather than replacing.
    Qt Code:
    1. m_scene->clear();
    2. m_scene->addPixmap(QPixmap::fromImage(GetImageFromDetector(ExpData.FileAndPath)));
    To copy to clipboard, switch view to plain text mode 

    solved the problem.
    Thanks a lot for making me think.
    Rno

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QImage.scanLine pointer memory leak [SOLVED]

    Thanks a lot for making me think.
    Thinking is one of those things we should all do from time to time. It would hurt so much less if we used our heads for thinking rather than banging them on the wall.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Qt & Memory Leak
    By fanoI in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2013, 08:53
  2. Pointer parameter in signals: Memory leak?
    By AlGaN in forum Qt Programming
    Replies: 3
    Last Post: 2nd December 2010, 10:24
  3. memory leak - where?
    By Tomasz in forum Newbie
    Replies: 36
    Last Post: 5th September 2010, 23:47
  4. memory leak when copying Qimage data
    By klobauster_toomc in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2010, 08:33
  5. QList<pointer>::takeFirst() and memory leak question
    By AcerExtensa in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2009, 13:20

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.