Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Memory Leak problem with QImage->copy()
Qt 6.11 is out! See what's new in the release blog

Memory Leak problem with QImage->copy()

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 5.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FjjF
    wrote on last edited by
    #1

    Hello, I'm lost again! please help:
    I'm trying to make a simple app in which a Spinner is the control to pan an image inside a QLabel.
    I make the QLabel show the image by setting the pixmap. When user clicks the spinner, I make a copy of the original image but clipped on the XY coordinates indicated by the spinners, how ever I foudn no way of releasing the memory consumed by the copy of the image so a memory leak is at hand! Resistance is futile! :( LOL.
    Any help would be really apreciated
    PS (Total newbie!)

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QImage>
    #include <QDebug>
    #include <QMessageBox>
    #include <QPixmap>
    #include <QLibrary>
    #include "scanner.h"
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setWindowTitle(tr("OMR"));

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_analizar_clicked()
    {
    image = new QImage("c:/omrtest.bmp");
    if(image->isNull()) {
    (new QMessageBox(QMessageBox::Critical,tr("Error"),tr("No se pudo cargar la imagen"),QMessageBox::Ok))->show();
    }

    //QImage *aux=new QImage(*image);
    Scanner *scanner=new Scanner((new QImage(*image)));
    

    }

    void MainWindow::on_xPos_valueChanged(int posX)
    {

    QPixmap *aux=new QPixmap();
    QImage img=image->copy(posX,2,100,100);
    aux->convertFromImage(img);
    ui->ojo->setPixmap(*aux);
    ui->ojo->show();
    

    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bhamuryen
      wrote on last edited by
      #2

      Hello,
      try this
      @ #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QImage>
      #include <QDebug>
      #include <QMessageBox>
      #include <QPixmap>
      #include <QLibrary>
      #include "scanner.h"

      QPixmap *aux;
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          this->setWindowTitle(tr("OMR"));
          aux=new QPixmap();     
      }
       
      MainWindow::~MainWindow()
      {
          delete ui;
          if(aux!=NULL)
              delete aux;
      }
       
      void MainWindow::on_analizar_clicked()
      {
          image = new QImage("c:/omrtest.bmp");
          if(image->isNull()) {
              (new QMessageBox(QMessageBox::Critical,tr("Error"),tr("No se pudo cargar la imagen"),QMessageBox::Ok))->show();
          }
       
          //QImage *aux=new QImage(*image);
          Scanner *scanner=new Scanner((new QImage(*image)));
      }
       
      void MainWindow::on_xPos_valueChanged(int posX)
      {
          QImage img=image->copy(posX,2,100,100);
          aux->convertFromImage(img);
          ui->ojo->setPixmap(*aux);
          ui->ojo->show();       
      }
      

      @

      c++ Software Developer

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You should avoid using pointer to QImages. Most of the time there are no needs for them. In fact in your code you always dereference them.
        i.e. not using a pointer for your image variable.
        @void MainWindow::on_xPos_valueChanged(int posX)
        {
        ui->ojo->setPixmap(QPixmap::fromImage(image.copy(posX, 2, 100, 100);
        ui->ojo->show();

        }@

        In on_analizar_clicked you have a double leak: Scanner is never destroyed an you pass it a new QImage that is also not destroyed. It also seems that you don't do anything with scanner.
        If scanner does some processing on your image without modifying it, you could simply pass it a const reference.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • F Offline
          F Offline
          FjjF
          wrote on last edited by
          #4

          Hi... I'll try your suggestions as soon as I can!. Meanwhile thanks for your replies!!!

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vittalonline
            wrote on last edited by
            #5

            you should follow "delete " for every pointer created by using "new" keyword.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved