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. QTableWidget memory leak problem
Forum Updated to NodeBB v4.3 + New Features

QTableWidget memory leak problem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.6k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    d2uriel
    wrote on last edited by
    #1

    Hi everyone,

    It's my first post here on the Qt forums so please do not bash me for my... lack of knowledge nor my poor English.
    I'm also a beginner in Qt - keep that in mind.

    Anyways, getting back on topic. I was debugging my Qt application written in C++ using Qt v4.6.3 and noticed that it has some memory leaks. I've searched through the source code and found that creating and destroying a QTableWidget object doesn't free all reserved memory.

    Here's sample code I've used to track this problem:

    test1.h
    @class test1 : public QMainWindow
    {
    Q_OBJECT

    public:
    test1(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~test1();

    private:
    QTableWidget *tableWidget;
    Ui::test1Class ui;

    public slots:
    void createTestObject();
    void removeTestObject();
    void memoryLeakCheck();
    };@

    test1.cpp
    @#include "test1.h"

    test1::test1(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    ui.setupUi(this);
    connect(ui.pushButton, SIGNAL(clicked()), SLOT(createTestObject()));
    connect(ui.pushButton_2, SIGNAL(clicked()), SLOT(removeTestObject()));
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), SLOT(memoryLeakCheck()));
    timer->start(1);
    }

    test1::~test1()
    {

    }

    void test1::createTestObject()
    {
    tableWidget = new QTableWidget(this);
    }

    void test1::removeTestObject()
    {
    delete tableWidget;
    }

    void test1::memoryLeakCheck()
    {
    createTestObject();
    //qDebug() << "Button created";
    removeTestObject();
    //qDebug() << "Button removed";
    }@

    Ui is an empty project with two QPushButtons. One creates a QTableWidget, the other one deletes it. As you can see I also used a timer to create and remove the objects automatically. If I have Windows Task Manager running I can clearly see that the memory usage for this test application doesn't stop growing. As far as I know it should stop after reaching some point with enough resources available to create a new QTableWidget object after deleting the previous one.

    I've Googled a bit and found nothing relevant to be honest. My question is: am I doing something wrong here or there actually is a memory leak in QTableWidget?

    I'm using Windows XP Pro and MS Visual Studio 2008 + Qt Visual Studio add-in to create, compile and run the projects if that makes any difference.

    Thank you in advance for your help.

    "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Depending on the use case the memory may be occupied by something else and thus the chunk is too small to fit a new QTableWidget.

      Despite this, looking at the Windows task manager, does not give you any serious information about the resource usage of a program. Something similar was discussed in the forum some time a go, you'll find it using the search.

      Anyways, I'm convinced that there are no serious memory leaks in the Qt libs, no need to worry.

      PS: Your code is dangerous, you should set the pointer to 0 after you delete it. Otherwise you have a dangling pointer.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • D Offline
        D Offline
        d2uriel
        wrote on last edited by
        #3

        The above code is just an example - it's not my app ;-). I wanted to keep it as simple as possible (that's why the pointer is never set to 0). How can this increasing memory usage can be explained then (within this simple app above)?

        If I do the same for a QPushButton (meaning instead of creating and deleting a QTableWidget I create and delete a QPushButton) the memory usage stays at one level throughout the application run time (not matter how long I run it).

        "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Hard to say. It depends on the actual application and to measure it accurately you will need some good analysis tool that shows you what object consume what memory.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • D Offline
            D Offline
            d2uriel
            wrote on last edited by
            #5

            Do you know any free analysis tool that will check if there actually is a memory leak?

            "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              "This link":http://developer.qt.nokia.com/search/tag/memory~leak, "this link":http://developer.qt.nokia.com/search?search=memory+profiling and "this link":http://developer.qt.nokia.com/search?search=memory+tracker give you further information on the topic.

              http://www.catb.org/~esr/faqs/smart-questions.html

              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