添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
侠义非凡的硬盘  ·  Linux/AM5728: OpenGL ...·  1 周前    · 
朝气蓬勃的饼干  ·  Installing from dev · ...·  1 月前    · 
玩命的小虾米  ·  SQL ...·  2 月前    · 
独立的帽子  ·  Unity 3D C#脚本基础·  4 月前    · 
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Welcome to Qt Centre .

Qt Centre is a community site devoted to programming in C++ using the Qt framework . Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue — register and post your question.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today !

If you have any problems with the registration process or your account login, please contact us .

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: Switch view
  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. }
void MainWindow::display_image()
    if(moving > 0) moving --;
    get_camera(ui->horizontalSlider_scan_size->value(), ui->horizontalSlider_colour_threshold->value());
    //--------------------------------------------------------------draw camera views
    if(ui->checkBox_vis->isChecked()) {
        ui->graphicsView_left_cam->setScene(left_cam_view);
        ui->graphicsView_left_cam->show();
        ui->graphicsView_left_cam->setSceneRect(0,0,image_width,image_height);
        limage = QImage(image_width, image_height, QImage::Format_ARGB32);
        #pragma omp parallel for
        for(int x=0; x<image_width; x++) {
            for(int y=0; y<image_height; y++) {
                QRgb lvalue = qRgba(left_image[x][y][0], left_image[x][y][1], left_image[x][y][2], 255);
                limage.setPixel(x,y,lvalue);
        lpixmap = QPixmap::fromImage(limage);
        left_cam_view->addPixmap(lpixmap);
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: Switch view
  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. }
void MainWindow::on_pushButton_start_clicked()
    ui->refresh->setDown(true);
    ui->refresh->setAutoRepeat(true);
    ui->refresh->setAutoRepeatInterval(50);
void MainWindow::on_refresh_clicked()
    display_image();
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 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). 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? 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: Switch view
  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. }
void MainWindow::display_image()
        lpixmap = QPixmap::fromImage(limage);
        if(myPixmapItem)
                delete myPixmapItem; 
        myPixmapItem= left_cam_view->addPixmap(lpixmap);
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: Switch view
  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. }
void MainWindow::init()
     myPixmapItem = new QGraphicsPixmapItem();
     left_cam_view->addItem(myPixmapItem);
void MainWindow::display_image()
    QPixmap lpixmap = QPixmap::fromImage(image);
    myPixmapItem->setPixmap(lpixmap);
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.