QTableWidget memory leak problem
-
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_OBJECTpublic:
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.
-
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.
-
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).
-
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.
-
Do you know any free analysis tool that will check if there actually is a memory leak?
-
"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.