从QTableView插入和删除值时的内存管理

时间:2018-11-19 11:40:17

标签: c++ qt memory-management qtableview qstandarditemmodel

在我的应用程序中,我需要在应用程序运行时多次从tableView中插入和删除数据。在从TableView内存中插入和删除数据的完整周期之后,如在任务管理器中检查的那样,我的应用程序的消耗增加了。在每个连续的插入和删除周期之后,内存消耗持续增加。

下面是我的代码:

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);


}

}

内存消耗从应用开始:

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

因此,在2个插入/删除循环中,Ram消耗增加了2 MB。

在我看来,这似乎是内存泄漏问题,因为内存已分配给QStandardItem对象(item = new QStandardItem(QString("%0").arg(value)))但从未被释放。我试图通过调用delete item来释放内存,但之后一行插入空白列。

增加内存消耗的可能原因是什么。

0 个答案:

没有答案