Memory Leak problem with QImage->copy()
-
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();
}
@ -
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(); }
@
-
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
-
Hi... I'll try your suggestions as soon as I can!. Meanwhile thanks for your replies!!!
-
you should follow "delete " for every pointer created by using "new" keyword.