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

william.crocker at analog.com william.crocker at analog.com
Thu Jul 8 17:02:58 CEST 2010


Daniel:

You need to add a tool like valgrind or purify($) to your belt.

Bill

Daniel Tripp wrote:
> 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
> 
> 
>       
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 



More information about the Qt-interest-old mailing list