0

In my Application I am required to insert and remove data from tableView multiple times while application is running.After a complete cycle of Inserting and removing data from tableView memory(RAM) consumption of my application increases as checked in task Manager. After each successive cycle of insertion and deletion memory consumption keeps on Increasing.

Below is my code:

MemoryLeak.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-11-19T16:00:26
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MemoryLeak
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

Main.cpp

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

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

    return a.exec();
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStandardItemModel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QStandardItemModel *model;

public slots:
    void createModel();
private slots:
    void on_clearModel_clicked();
    void on_displayData_clicked();
};

#endif // MAINWINDOW_H

MainWindow.cpp

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    createModel();
}

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

void MainWindow::createModel(){
model=new QStandardItemModel(this);
model->setColumnCount(3);
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Age"));
model->setHeaderData(2, Qt::Horizontal, tr("Class"));

ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->tableView->horizontalHeader()->resizeSection(0,580);
ui->tableView->horizontalHeader()->resizeSection(1,280);
ui->tableView->horizontalHeader()->resizeSection(2,280);

ui->tableView->setAlternatingRowColors(true);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setStyleSheet("alternate-background-color:#00FF00;border: 1px solid red;");

}



void MainWindow::on_clearModel_clicked()
{
    model->setRowCount(0);

}

void MainWindow::on_displayData_clicked()
{
    int rowCount = 0;
    QStandardItem *item;
    QString value;

    for(rowCount = 0; rowCount < 5000; rowCount ++){


    value = QString("Person_%1").arg(rowCount);
    item = new QStandardItem(QString("%0").arg(value));
    model->setItem(rowCount,0,item);

   // delete item;// No value is inserted in column


    item = new QStandardItem(QString(" %0").arg(rowCount));
    model->setItem(rowCount,1,item);


    item = new QStandardItem(QString("%0").arg(2));
    model->setItem(rowCount,2,item);


}

}

Memory consumption From app start:

11020 KB when app started

16144 KB when display data function Called

12812 KB when clear model called

16356 KB when display data function Called

13304 KB when clear Model Called

So Ram consumption increased by 2 MB in 2 Insert/Remove cycles.

To me it seems to be memory leak issue because memory is allocated to QStandardItem object( item = new QStandardItem(QString("%0").arg(value))) but is never freed.I tried to free the memory by calling delete item but after it a row is inserted with blank column.

What might be the possible reason for increase in memory consumption.

Asus gates
  • 380
  • 1
  • 5
  • 20
  • Task Manager is not the correct tool to analyze memory consumption. read https://stackoverflow.com/questions/4690800/how-to-profile-memory-usage – eyllanesc Nov 19 '18 at 11:43
  • @eyllanesc which tool do you suggest. for precise result – Asus gates Nov 19 '18 at 11:44
  • read the link... – eyllanesc Nov 19 '18 at 11:44
  • 1
    Why do you call `model->setRowCount(0);` function instead of calling `model::clear()` function? – vahancho Nov 19 '18 at 11:44
  • @vahancho model::clear also clear my header data – Asus gates Nov 19 '18 at 11:46
  • @vahancho Memory consumption remains same if i Use model::clear() insted of model->setRowCount(0) – Asus gates Nov 19 '18 at 11:51
  • @Asusgates You keep calling `new` left and right and you don't care where the result goes, and you're surprised there's a memory leak? Are you aware of where every `new`ly allocated object goes after you clear? – The Quantum Physicist Nov 19 '18 at 11:52
  • @TheQuantumPhysicist I think it keeps lying on heap – Asus gates Nov 19 '18 at 11:56
  • @TheQuantumPhysicist When and where should I clear the QStandardItem object allocated on Heap. – Asus gates Nov 19 '18 at 11:59
  • You should have a data model that maps to your table. Please refer [this tutorial](http://doc.qt.io/qt-5/model-view-programming.html) to understand how model/view programming works in Qt. You should not be allocating these yourself. – The Quantum Physicist Nov 19 '18 at 12:07
  • @TheQuantumPhysicist doesn't Qt automatically/implicitly handle dynamic allocation of Qt objects? They consistently use `new` in [`QStandardItemModel` snippets](http://doc.qt.io/qt-5/qstandarditemmodel.html#details) and then there are Qt's [object trees](http://doc.qt.io/qt-5/objecttrees.html). – TrebledJ Nov 19 '18 at 19:21
  • @TrebuchetMS Yes but you have to define the parents very carefully and understand the hierarchy, which 1. doesn't look like the case here, and 2. there's no parents in any of these objects. It's not automatic like it's with smart pointers. On the other hand, the use here is just wrong. Model/view programming is not done like in the example. – The Quantum Physicist Nov 19 '18 at 20:19

0 Answers0