[Qt-interest] Qt Memory Leak (or Fragmentation?)

Daniel Tripp tripp.daniel at rocketmail.com
Thu Jul 8 16:48:36 CEST 2010


I'm running into a memory issue with Qt. If the text of a QLabel changes, it 
slowly eats memory. I don't see any apparent leaks with the my test code, but 
here it is just in case. Does anyone know how to prevent this?


main.cpp:


#include "labeltest.h"
#include <QApplication>
int main(int argc, char *argv[]) 
{
     QApplication a(argc, argv);
     LabelTest test;
     test.show();
     return a.exec();
} 

labeltest.cpp:
 
#include "labeltest.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>

LabelTest::LabelTest(QWidget *parent) : QWidget(parent)
{
     shortWord = false;
     layout = new QVBoxLayout(this);
     randomLabel = new QLabel(this);
     updateLabel();
     layout->addWidget(randomLabel);
     button = new QPushButton(this);
     button->setText("Click Me");     
     button->setFocus();
     layout->addWidget(button);
      connect(button, SIGNAL(clicked()), this, SLOT(updateLabel()));
}
void LabelTest::updateLabel() 
{
     if(shortWord)
         randomLabel->setText("Short string");
     else
         randomLabel->setText("This is a considerably longer string");
     shortWord = !shortWord;
} 

labeltest.h:

#ifndef LABELTEST
#define LABELTEST

#include <QWidget>
#include <QList>
class QLabel;
class QPushButton;
class QVBoxLayout;

class LabelTest : public QWidget 
{
     Q_OBJECT
public:
     LabelTest(QWidget *parent = 0);
private:
     QLabel *randomLabel;
     QPushButton *button;
     QVBoxLayout *layout;
     bool shortWord;
private slots:
     void updateLabel();
};
#endif 

labeltest.pro:


SOURCES += main.cpp \
            labeltest.cpp
HEADERS += labeltest.h


      




More information about the Qt-interest-old mailing list