QTableWidgetItem(or QTableWidget) memory leak problem,,,



  • Hi, everyone.

    While I was testing my QT application, I found that 'memory usage' of my application on 'windows task manager' is keep increasing and never goes down. I spend a few ours to find out what causes this memory leak, and, barely notice that update of QTableWegetItem causes this problem.
    I know it's ridiculous. So I made a simple test code, and it has a same problem.
    These are my test code.
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QVBoxLayout>
    #include <QTableWidget>
    #include <QPushButton>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    QPushButton *button1;
    QWidget *centralWidget;
    QVBoxLayout *mainLayout;
    QTableWidget *tableWidget;

    private slots:
    void slotTest1Clicked();

    };

    #endif // MAINWINDOW_H
    @

    @#include "mainwindow.h"
    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    button1 = new QPushButton(tr("test1"));
    connect(button1, SIGNAL(clicked()), this, SLOT(slotTest1Clicked()));
    tableWidget = new QTableWidget(1000, 10, this);

    for(int i = 0; i < tableWidget->rowCount(); ++i)
    {
        for(int j = 0; j < tableWidget->columnCount(); ++j)
        {
            QTableWidgetItem * item = new QTableWidgetItem();
            tableWidget->setItem(i, j, item);
        }
    }
    
    mainLayout = new QVBoxLayout();
    mainLayout->addWidget(button1);
    mainLayout->addWidget(tableWidget);
    
    centralWidget = new QWidget();
    centralWidget->setLayout(mainLayout);
    
    setCentralWidget(centralWidget);
    

    }

    MainWindow::~MainWindow()
    {
    if(button1) delete button1;
    if(tableWidget) delete tableWidget;
    if(mainLayout) delete mainLayout;
    if(centralWidget) delete centralWidget;
    }

    void MainWindow::slotTest1Clicked()
    {
    static int x = 0;
    for(int i = 0; i < tableWidget->rowCount(); ++i)
    {
    for(int j = 0; j < tableWidget->columnCount(); ++j)
    {
    //tableWidget->item(i, j)->setData(Qt::EditRole, j);
    tableWidget->item(i, j)->setData(Qt::EditRole, j + x);
    }
    }
    ++x;
    }@

    Very simple, isn't it?
    To see obvious increment, I use 10000 contents.
    Every time I push the 'button1', every contents are updated. (Numbers in cells increases.)
    Every time I push the button, the memory usage increases 400 ~ 700 kbyte.

    I build it as a release version, and run it without QT Creator.
    My developing environments are

    • Windows 7 Professional K 64bit Service Pack 1
    • Qt Creator 2.7.0 Based on Qt 5.0.2 (32 bit)

    Funny thing is, if I use line #45 instead of #46, (tableWidget->item(i, j)->setData(Qt::EditRole, j);)
    memory does not increase, even though same setItem() function is called.

    This is my first QT application, so, I'm not good at QT yet.
    Please, tell me why this memory leak is happen, and how can I avoid it.
    Any comments are welcome.

    Kim



  • Looks simple. When you deleted the table, did you observe memory back to normal. If memory does not get released ever after deleting the table, then we can term it as memory leak. Otherwise I don't see any issue. It may be memory is required. j+x may be creating the temporary object causing the memory to go up.



  • I added one more button to delete that QTableWidget, and it doesn't release a memory at all. I changed slotTest1Clicked more simple.
    @void MainWindow::slotTest1Clicked()
    {
    static int x = 0;
    for(int i = 0; i < tableWidget->rowCount(); ++i)
    {
    for(int j = 0; j < tableWidget->columnCount(); ++j)
    {
    tableWidget->item(i, j)->setData(Qt::EditRole, x);
    }
    }
    ++x;
    }@
    But, still, it has a same problem. The beginning memory usage is about 6.9 mbytes. After I push the 'test1' button 100 times, the memory usage goes to about 70 mbytes. (It increases every time when i push the button.) Even though I delete QTableWidget using my new button, it doesn't decrease.



  • I just tried with this following code on Win8 32 bit MinGW compiler with Qt 5.2.0. It works as expected. Once I delete memory allocated for item gets released automatically. Also memory increase and decrease is close 500K. It works consistently.

    @void MainWindow::slot2()
    {
    for(int i = 0; i < tableWidget->rowCount(); ++i)
    {
    for(int j = 0; j < tableWidget->columnCount(); ++j)
    {
    //tableWidget->item(i, j)->setData(Qt::EditRole, j);
    QTableWidgetItem *item = tableWidget->item(i, j);
    delete item;
    }
    }

    void MainWindow::slot2()
    {
    delete tableWidget;
    }
    }@



  • Thank you for your help. The memory leak occurred on Qt 5.0.2 and the compiler was MSVC 2012. I've changed the compiler to MinGW and the memory leak has not been occurred anymore. I think it's a environmental(?) problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.