Results 1 to 5 of 5

Thread: QScrollArea Memory Leak

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default QScrollArea Memory Leak

    Hey, I'm currently using Qt Embedded 4.5.1. I'm using a QScrollArea to contain a QLabel. Each time I render the QScrollArea, the process eats up more memory. Is there a way I can prevent this? Is there something I'm doing incorrect?

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    2. {
    3. buildWidget();
    4. retranslateWidget();
    5. }
    6.  
    7. void MyWidget::buildWidget()
    8. {
    9. layout = new QVBoxLayout;
    10. scroll = new QScrollArea;
    11.  
    12. scroll->setWidgetResizable(true);
    13. scroll->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
    14. scroll->setFrameShape(QFrame::NoFrame);
    15. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    16.  
    17. text = new QLabel;
    18. text->setWordWrap(true);
    19.  
    20. scroll->setWidget(text);
    21.  
    22. layout->addWidget(scroll);
    23.  
    24. setLayout(layout);
    25. }
    26.  
    27. void MyWidget::retranslateWidget()
    28. {
    29. aboutText->setText(tr("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."));
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea Memory Leak

    Well, if this is a wanted behavior to create a new label each time in buildWidget(), then its not memory leak.
    However some remarks:
    1. you should do 'text = new QLable(this);' so that the pointer gets deleted when the parent gets deleted, OR, explicitly delete 'text' when its not needed any more.
    If you didn't do that, then THAT is a memory leak.
    2. If you don't really want to create a new label each time, than make it a member and either create in once in the contructor, or check to see if it is not yet created and only then allocate it like so:
    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    2. ,text(NULL)
    3. {
    4. ...
    5. }
    6.  
    7. void MyWidget::buildWidget()
    8. {
    9. ...
    10. if(!text)
    11. text = new QLabel(this);
    12.  
    13. {
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea Memory Leak

    The label will get reparented to the scroll area after the setWidget() call so no need to delete it explicitely. I would be interested in knowing how the OP knows the application eats up memory after each redraw of the widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4

    Default Re: QScrollArea Memory Leak

    I can watch the memory consumption of the process by calling "watch /proc/(pid)/status". I found a few more interesting things. This problem does not arise when placing a QPixmap in the QLabel instead of text. Also, if I hide() the QScrollArea, the problem goes away as well.

    Here's the full code for the app:

    mywidget.h
    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QLabel;
    7.  
    8. class MyWidget : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MyWidget(QWidget *parent = 0);
    14.  
    15. private:
    16. void buildWidget();
    17. void retranslateWidget();
    18.  
    19. QVBoxLayout *layout;
    20. QScrollArea *scroll;
    21. QLabel *text;
    22. QPushButton *button;
    23.  
    24. private slots:
    25. void rebuildScrollArea();
    26. };
    27.  
    28. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    mywidget.cpp
    Qt Code:
    1. #include "mywidget.h"
    2.  
    3. #include <QScrollArea>
    4. #include <QVBoxLayout>
    5. #include <QLabel>
    6. #include <QPushButton>
    7. #include <QApplication>
    8. #include <QImage>
    9.  
    10. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    11. {
    12. scroll = NULL;
    13.  
    14. setFixedHeight(240);
    15. setFixedWidth(320);
    16.  
    17. buildWidget();
    18.  
    19. connect(button, SIGNAL(clicked()), this, SLOT(rebuildScrollArea()));
    20. }
    21.  
    22. void MyWidget::buildWidget()
    23. {
    24. layout = new QVBoxLayout;
    25. setLayout(layout);
    26.  
    27. button = new QPushButton;
    28. layout->addWidget(button);
    29.  
    30. rebuildScrollArea();
    31. }
    32.  
    33. void MyWidget::rebuildScrollArea()
    34. {
    35. if (scroll) scroll->deleteLater();
    36.  
    37. scroll = new QScrollArea;
    38. scroll->setWidgetResizable(true);
    39. //scroll->hide();
    40.  
    41. text = new QLabel;
    42. text->setWordWrap(true);
    43. //text->setPixmap(QPixmap(":/stewart.png"));
    44. //text->adjustSize();
    45.  
    46. scroll->setWidget(text);
    47.  
    48. layout->addWidget(scroll);
    49.  
    50. retranslateWidget();
    51. }
    52.  
    53. void MyWidget::retranslateWidget()
    54. {
    55. button->setText(tr("Click to Rebuild"));
    56. text->setText(tr("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    57. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    58. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."));
    59.  
    60. }
    To copy to clipboard, switch view to plain text mode 

    main.ccp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mywidget.h"
    3. #include "zdefaultstyle.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MyWidget w;
    9.  
    10. #if defined(Q_WS_S60)
    11. w.showMaximized();
    12. #else
    13. w.show();
    14. #endif
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea Memory Leak

    This problem does not arise when placing a QPixmap in the QLabel instead of text.
    That is probably because the QLabel is allocated on the heap, and the QPixmap on the stack, and the stack size is fixed, and 'watch' probably is looking at the dynamic memory allocation.

    I still fail to understand the problem.
    Allocating new object consumes more memory, that is the nature of things.
    Each time I render the QScrollArea, the process eats up more memory.
    As long as 'MyWidget' object is alive, it only allocates new QLabels.
    If each time you render the QScrollArea its done with the same 'MyWidget' object (with rebuildScrollArea()), then you allocate new memory.
    You also should see a new QLabel in your scroll area each time you call rebuildScrollArea().

    Also, if I hide() the QScrollArea, the problem goes away as well.
    Maybe its because you call rebuildScrollArea() as part of the paint sequence, so it is not called when the widget is not visible.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. memory leak - where?
    By Tomasz in forum Newbie
    Replies: 36
    Last Post: 5th September 2010, 23:47
  2. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  3. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  4. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31
  5. Memory leak
    By zlatko in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2006, 19:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.