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.