Pregunta

I observed that through task mgr, the memory increases in steps of 4kB and 8kB, though not necessarily in this order.

Possible duplicate: Windows Task Manager shows process memory keeps growing even though there are no memory leaks

I am not sure whether this's occurring because I did not release the QTimer object, timer2. Please advise me how to stop this memory increase, and whether my guess of why it's occurring, is correct.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCore>
#include <QDebug>
#include <QDateTime>
#include <QFileInfo>
#include <QString>
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#define TIMER2_VALUE 3000
#define UPDATED_IMAGE_STORAGE_PATH "E:\\QT1\\timeStampDateMod2\\TimeStampDateMod2\\updatedRefImg.JPG"
#define UPDATED_IMAGE_BACKUP_PATH "E:\\QT1\\timeStampDateMod2\\TimeStampDateMod2\\backUp\\updatedRefImg[%1].JPG"

using namespace std;
using namespace cv;

typedef struct
{
     QDateTime dateTimeMod1;
     QDateTime dateTimeMod2;
}tTimeMods;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QTimer *timer2;
    tTimeMods findTimeModifiedStruct();
    QDateTime findTimeModified();
    void compareTimeMods(tTimeMods timeTypeFunction, QDateTime dateTimeMod2);
    QString appendWithImageName(tTimeMods timeTypeFunction);
    void shiftToRepository(QString pathString);
    void updatedImgToRepository(QString pathString);

public slots:
    void timerSlot2();

private:
    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

tTimeMods timeTypeFunction, timeTypeMain;
QDateTime dateTimeMod2;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timeTypeMain = findTimeModifiedStruct();
    timer2 = new QTimer(this);
    connect(timer2, SIGNAL(timeout()), this, SLOT(timerSlot2()));
    timer2->start(TIMER2_VALUE);
}

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

void MainWindow::timerSlot2()
{
    dateTimeMod2 = findTimeModified();
    compareTimeMods(timeTypeMain, dateTimeMod2);
    //delete timer2;
}

//tTimeMods findTimeModifiedStruct()
tTimeMods MainWindow::findTimeModifiedStruct()
{
    QString myFileName = UPDATED_IMAGE_STORAGE_PATH;
    QFileInfo info(myFileName);

    /*find last date modified*/
    timeTypeFunction.dateTimeMod1 = info.lastModified();
    timeTypeFunction.dateTimeMod2 = info.lastModified();

    qDebug()<< "dateTimeMod1: " << timeTypeFunction.dateTimeMod1.toString() << endl << "dateTimeMod2: "<< timeTypeFunction.dateTimeMod2.toString();
    return(timeTypeFunction);
}

QDateTime MainWindow::findTimeModified()
{
    QString myFileName = UPDATED_IMAGE_STORAGE_PATH;
    QFileInfo info(myFileName);

    QDateTime dateTimeMod2 = info.lastModified();
    qDebug()<< "dateTimeMod2: "<< dateTimeMod2.toString();
    return(dateTimeMod2);
}

void MainWindow::compareTimeMods(tTimeMods timeTypeFunction, QDateTime dateTimeMod2)
{
    if(dateTimeMod2 >= timeTypeFunction.dateTimeMod1)
    {
        timeTypeFunction.dateTimeMod1 = dateTimeMod2;
        QString pathString = appendWithImageName(timeTypeFunction);
        shiftToRepository(pathString);
    }
}

QString MainWindow::appendWithImageName(tTimeMods timeTypeFunction)
{
   /*appending just the timeMod with the path & image name*/
   QString path = QString(UPDATED_IMAGE_BACKUP_PATH).arg(timeTypeFunction.dateTimeMod1.toString());
   qDebug()<< "path: " << path;
   return path;
}

void MainWindow::shiftToRepository(QString pathString)
{
    updatedImgToRepository(pathString);
}

void MainWindow::updatedImgToRepository(QString pathString)
{
    pathString.replace(":","-");
    pathString.replace(1,1,":");
    qDebug()<<"pathString now: "<<pathString;

    /*convert QString into char* */
    QByteArray pathByteArray = pathString.toLocal8Bit();
    const char *path = pathByteArray.data();

    IplImage *InputImg = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
    InputImg = cvLoadImage(UPDATED_IMAGE_STORAGE_PATH ,CV_LOAD_IMAGE_UNCHANGED);

    /*save the image*/
    cvSaveImage(path,InputImg);
    cvReleaseImage(&InputImg);
}
¿Fue útil?

Solución

I'm not familiar with OpenCV, but it seems that these two lines cause you memory leak:

IplImage *InputImg = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
InputImg = cvLoadImage(UPDATED_IMAGE_STORAGE_PATH ,CV_LOAD_IMAGE_UNCHANGED);

In the first line you are creating an object, but in the second you are assigning another object to the pointer without releasing the previous one, so the previous one gets leaked.

Is creating an image even needed, while you are going to load it?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top