Results 1 to 8 of 8

Thread: Displaying pixmaps and memory leak ??

  1. #1
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Displaying pixmaps and memory leak ??

    Hi,

    I'm fairly new to qt, I'm using qt creator on both linux and mac and the same code is causing the same problem on both platforms. Anyway I'm trying to display an image from a camera and while it is displaying it's also causing a memory leak which over time eventually crashes the computer. I've had a look online but can't seem to find anything on this. If I comment out the code that displays the image then the rest of the program works fine with no memory leak and no crash.

    Qt Code:
    1. void MainWindow::display_image()
    2. {
    3. if(moving > 0) moving --;
    4. get_camera(ui->horizontalSlider_scan_size->value(), ui->horizontalSlider_colour_threshold->value());
    5.  
    6. //--------------------------------------------------------------draw camera views
    7. if(ui->checkBox_vis->isChecked()) {
    8. ui->graphicsView_left_cam->setScene(left_cam_view);
    9. ui->graphicsView_left_cam->show();
    10. ui->graphicsView_left_cam->setSceneRect(0,0,image_width,image_height);
    11. limage = QImage(image_width, image_height, QImage::Format_ARGB32);
    12.  
    13. #pragma omp parallel for
    14. for(int x=0; x<image_width; x++) {
    15. for(int y=0; y<image_height; y++) {
    16. QRgb lvalue = qRgba(left_image[x][y][0], left_image[x][y][1], left_image[x][y][2], 255);
    17. limage.setPixel(x,y,lvalue);
    18. }
    19. }
    20.  
    21. lpixmap = QPixmap::fromImage(limage);
    22. left_cam_view->addPixmap(lpixmap);
    23. }
    To copy to clipboard, switch view to plain text mode 

    So whats going on here is... I have an image which is stored in a matrix x by y by 3 (r,g,b) so I set the value of a pixel and then set that pixel in a Qimage.

    So the user presses a button to start things off and that causes a hidden button to repeat which calls this function...

    Qt Code:
    1. void MainWindow::on_pushButton_start_clicked()
    2. {
    3. ui->refresh->setDown(true);
    4. ui->refresh->setAutoRepeat(true);
    5. ui->refresh->setAutoRepeatInterval(50);
    6. }
    7.  
    8. void MainWindow::on_refresh_clicked()
    9. {
    10. display_image();
    11. }
    To copy to clipboard, switch view to plain text mode 


    Thats it, so why do I get the memory leak? what have I missed or misunderstood about this?

    Cheers
    Last edited by tonytony; 28th June 2010 at 12:44.

  2. #2
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying pixmaps and memory leak ??

    Can you show the implementation of left_cam_view->addPixmap(lpixmap);?
    I see no dynamic allocations here, are you sure that the crash is caused by a memory leaks?

  3. #3
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Displaying pixmaps and memory leak ??

    Hmm I'm not sure what you mean by the implementation of left_cam_view->addPixmap(lpixmap);
    This is part of qt creator. The initializations are...

    QGraphicsScene *left_cam_view;
    QImage limage;
    QPixmap lpixmap;

    As for the memory leak, if ui->checkBox_vis->isChecked() is not checked so the first function doesn't do the drawing bit then the program memory is constant and it can run a long time with no problem.
    When it is checked so the drawing function is used then the memory usage fluctuates and generally goes up, but over time the overall memory in use by programs goes up hugely and keeps going up until first the program slows down and finally the computer freezes. Quitting the program and the shell from which it is launched does not free this memory. In fact from the system monitor it shows that while GiB's are in use by programs, the memory in use by processes is not that much.

    From all this it would seem that the problem is due to this small bit of code (as without it there is no problem).

  4. #4
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Displaying pixmaps and memory leak ??

    Are you sure calling QGraphicsScene::addPixmap() will clear the previous one?
    The Qt Assistant doesn't say anything about this, but if it doesn't clear the previous QPixmap, it definitely is a leak.
    It's not the goodbye that hurts,but the flashback that follow.

  5. #5
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Displaying pixmaps and memory leak ??

    Quote Originally Posted by MorrisLiang View Post
    Are you sure calling QGraphicsScene::addPixmap() will clear the previous one?
    The Qt Assistant doesn't say anything about this, but if it doesn't clear the previous QPixmap, it definitely is a leak.
    This is pretty much what I thought might be the problem but I have no idea how to fix it...

  6. #6
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Displaying pixmaps and memory leak ??

    Ok so clearly this is not the way to do things...

    I'm struggling to work out how to draw a QImage or QPixmap onto the GUI though. I can find loads of examples but they are not QT creator applications using the QGraphicsView object. I can set the scene etc but how to set it to use my image without using something like addPixmap which doesn't remove the old pixmap?

  7. #7
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Displaying pixmaps and memory leak ??

    Basically, calling QGraphicsScene::addPixmap() will return a QGraphicsPixmapItem. So all you have to do is to hold the QGraphicsPixmapItem pointer when adding a pixmap. When adding another pixmap, delete the previous QGraphicsPixmapItem first.

    Qt Code:
    1. void MainWindow::display_image()
    2. {
    3. ...
    4. lpixmap = QPixmap::fromImage(limage);
    5. if(myPixmapItem)
    6. delete myPixmapItem;
    7. myPixmapItem= left_cam_view->addPixmap(lpixmap);
    8. }
    To copy to clipboard, switch view to plain text mode 

    the myPixmapItem is a member of MainWindow, a pointer to QGraphicsPixmapItem. Remember to set it to 0 when constructing a MainWindow Object.



    Another way is,
    Qt Code:
    1. void MainWindow::init()
    2. {
    3. ...
    4. myPixmapItem = new QGraphicsPixmapItem();
    5. left_cam_view->addItem(myPixmapItem);
    6. ...
    7. }
    8.  
    9. void MainWindow::display_image()
    10. {
    11. ...
    12. QPixmap lpixmap = QPixmap::fromImage(image);
    13. myPixmapItem->setPixmap(lpixmap);
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 
    In this way, you will only have a QGraphicsPixmapItem and no more creating and deleting. This one would be better solution. I'm sure calling QGraphicsPixmapItem::setPixmap() will clear the previous QPixmap.

    Some other things are that you're creating a QImage and a QPixmap in the stack every time the display_image() is called. This will cause constructing and destructing them. I think it's better to make them members of the MainWindow. And then you don't have to construct and destruct them. It shouldn't take too much memory, since QPixmap is implicit sharing.
    Last edited by MorrisLiang; 29th June 2010 at 09:59.
    It's not the goodbye that hurts,but the flashback that follow.

  8. The following user says thank you to MorrisLiang for this useful post:

    SiddhantR (2nd June 2014)

  9. #8
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Displaying pixmaps and memory leak ??

    Ok so before I saw the last post I discovered that clearing the scene fixed the problem, but it looks like the last post gives a more elegant solution. Thanks for you help with this!

Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  2. Memory leak of Qt?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2009, 23:32
  3. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  4. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  5. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31

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.