Results 1 to 4 of 4

Thread: QImage buffer

  1. #1
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QImage buffer

    Hi, I'm using this code:

    Qt Code:
    1. QImage img(str);
    2. if(img.isNull()) {
    3. printf("Failed to open image\n");
    4. return NULL;
    5. }
    To copy to clipboard, switch view to plain text mode 

    but it seems that QImage doesn't delete the buffer when destroyed (at the end of the method where this block of code is in). I don't use the QImage to store my images (I convert to another structure)... so, I can't free this buffer memory and the system becomes with a lot of buffer memory without usage (I use a huge amount of images). Any idea? I'd like to get rid of this buffer.

    Thanks, Rafael
    Last edited by jpn; 23rd June 2008 at 19:43. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage buffer

    How do you check that? Relevant thread: It seems that Qt does not release unused memory
    J-P Nurmi

  3. #3
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage buffer

    Let me explain what happens using top:

    First, I run my program and this is the memory usage:
    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    11180 rafael 15 0 45424 26m 8372 S 0.0 5.3 0:00.56 AVP
    After execute this code:

    Qt Code:
    1. FrameRGB * Loaders::qt_loader(QString str) {
    2. QImage img(str);
    3. if(img.isNull()) {
    4. printf("Failed to open image\n");
    5. return NULL;
    6. }
    7. int w = img.width();
    8. int h = img.height();
    9. FrameRGB *res = new FrameRGB(w, h);
    10. for(int y = 0; y < h; y++) {
    11. for(int x = 0; x < w; x++) {
    12. int color = img.pixel(x,y);
    13. int R = qRed(color);
    14. int G = qGreen(color);
    15. int B = qBlue(color);
    16. res->simpleSetPixel(y, x, PixelRGB(R, G, B));
    17. }
    18. }
    19. return res;
    20. }
    To copy to clipboard, switch view to plain text mode 
    This function loads an image using QImage... and the result is:

    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    11180 rafael 18 0 153m 121m 9928 S 0.0 24.1 0:04.78 AVP
    In theory, after done some operations, the memory would back to ~45m... but it doesn't

    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    11180 rafael 15 0 151m 119m 9928 S 0.0 23.7 0:05.23 AVP
    But, if I change this code to:

    Qt Code:
    1. FrameRGB * Loaders::qt_loader(QString str) {
    2. int w = 320;
    3. int h = 240;
    4. FrameRGB *res = new FrameRGB(w, h);
    5. for(int y = 0; y < h; y++) {
    6. for(int x = 0; x < w; x++) {
    7. res->simpleSetPixel(y, x, PixelRGB(0, 0, 0));
    8. }
    9. }
    10. return res;
    11. }
    To copy to clipboard, switch view to plain text mode 
    In other words, I discarded the QImage loading method and replaced by a constant flat color. In this case, the memory comes back to ~45m! So, I deduced that something is wrong with the QImage.
    Last edited by jpn; 24th June 2008 at 06:33. Reason: missing tags

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage buffer

    QImage works fine - it's your method to measure the memory usage which is wrong. As long as valgrind (or a similar tool) doesn't report a memory leak inside QImage you don't have to worry about it.

Similar Threads

  1. [Qt4] How to load Url image into QImage?
    By Gonzalez in forum Qt Programming
    Replies: 6
    Last Post: 13th October 2014, 10:10
  2. Replies: 12
    Last Post: 7th September 2011, 16:37
  3. How to reuse a QImage in a While loop?
    By JoseTlaseca in forum Qt Programming
    Replies: 9
    Last Post: 5th June 2008, 07:19
  4. QImage constructor
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 12:06
  5. QImage from 8 bit char* RGB buffer
    By tboloo in forum Qt Programming
    Replies: 13
    Last Post: 15th April 2006, 19:56

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.