首页 > 解决方案 > 从 QTableView 插入和删除值时的内存管理

问题描述

在我的应用程序中,我需要在应用程序运行时多次从 tableView 插入和删除数据。在从 tableView 内存(RAM)插入和删除数据的完整周期后,我的应用程序的消耗会增加,如在任务管理器中检查。在插入和删除的每个连续循环之后,内存消耗不断增加。

下面是我的代码:

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

主文件

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

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

    return a.exec();
}

主窗口.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

主窗口.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 个插入/删除周期中,内存消耗增加了 2 MB。

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

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

标签: c++qtmemory-managementqtableviewqstandarditemmodel

解决方案


推荐阅读