Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidgetItem(or QTableWidget) memory leak problem,,,

QTableWidgetItem(or QTableWidget) memory leak problem,,,

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yckim
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yckim
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          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;
          }
          }@

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yckim
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved