Memory leaks in QPushButton or what? [moved to bugtracker]



  • Shortly: when i move the mouse over the QPushButton in MS Windows my app eats some memory.
    But if I close the widget where the buttons are then memory will be returned.

    This is my test.app.
    Launch, push button "show widget2", move mouse over buttons on widget some times and see memory usage in task manager.

    Forgot to specify that I'm using qt5

    @
    //-- widget1.h
    class Widget1 : public QWidget
    {
    Q_OBJECT
    private:
    void callWidget2();
    public:
    Widget1(QWidget* parent = 0);
    ~Widget1() {}

    };

    //-- widget1.cpp
    Widget1::Widget1(QWidget* parent)
    :QWidget(parent)
    {
    QPushButton* btn = new QPushButton(this);
    btn->setText("show widget2");
    connect(btn, &QPushButton::clicked, this, &Widget1::callWidget2);

    this->resize(100, 100);

    this->show();
    }

    void Widget1::callWidget2()
    {
    new Widget2();
    }

    //-- widget2.h
    class Widget2 : public QWidget
    {
    Q_OBJECT
    private:

    public:
    Widget2(QWidget* parent = 0);
    ~Widget2() {}

    };

    //-- widget2.cpp
    Widget2::Widget2(QWidget* parent)
    :QWidget(parent)
    {
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->resize(300, 300);

    QPushButton* button = new QPushButton(this);
    button->setText("btn1");

    button = new QPushButton(this);
    button->setText("btn2");
    button->move(0, 20);

    button = new QPushButton(this);
    button->setText("btn3");
    button->move(0, 40);

    button = new QPushButton(this);
    button->setText("btn4");
    button->move(0, 60);

    button = new QPushButton(this);
    button->setText("btn5");
    button->move(0, 80);

    this->show();
    }

    //-- main.cpp
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    new Widget1;

    return a.exec();
    }
    @

    !http://anaks-home.ru/files_download/files/SNAG-0007.png (2-3minutes of test)!



  • This method creates a new object on the heap, but doesn't store the pointer anywhere!
    @void Widget1::callWidget2()
    {
    new Widget2();
    }@

    Consequently this object can never get destroyed → memory leak! Oups ;-)

    Same mistake in your Main function:
    @int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    new Widget1; //memory leak !!!

    return a.exec();
    }@

    Either create those objects on the stack (so they get destroyed automatically, as soon as the function goes out of scope) or store the pointer and delete it manually as soon as the object isn't needed anymore.



  • Nope. memory usage grows when I just move the mouse over the buttons.



  • Use software for detecting memory leaks.



  • [quote author="anaksimandr" date="1355064940"]Nope. memory usage grows when I just move the mouse over the buttons.[/quote]

    Does it still happen after you fix the obvious memory leaks I pointed out?



  • bq. Use software for detecting memory leaks.

    It shows that the leak is not in my code.

    bq. Does it still happen after you fix the obvious memory leaks I pointed out?

    How it may affect what I'm saying? There is no way.
    These widgets are created once.



  • [quote author="anaksimandr" date="1355070814"]bq. Use software for detecting memory leaks.

    It shows that the leak is not in my code.[/quote]

    What tool did you use? And what exactly did it detect?

    You say you are using Qt5. Qt5 is still under development. So do you use the latest Release Candidate?

    If so and if you have a meaningful leak report from your tool, you should open a BugTracker ticket.

    Otherwise, try updating first...

    [quote author="anaksimandr" date="1355070814"]bq. Does it still happen after you fix the obvious memory leaks I pointed out?

    How it may affect what I'm saying? There is no way.
    These widgets are created once.[/quote]

    If you think you have a memory leak somewhere and there is a very obvious leak in your own code, it would be logically to fix the obvious leak first and then check again whether there still indication for another leak...



  • What are you talking about? I push the button only one time and the widgets creates only one time so there is no leak in it. Do not waste my time with empty speculation. I don't want to talk about it more.

    I don't open a bug ticket because memory freed when I close the widget where the buttons are so it's strange (not simple leak). Dragging mouse over the buttons eats memory but the memory frees when the buttons destroys.

    I'm using qt5 beta2 and VC++ with crtdbg.h. It shows something like

    {8158} normal block at 0x036FE050, 24 bytes long.
    Data: <- o > 2D 91 C1 CD CE CD CD CD 01 00 00 C0 A8 E0 6F 03
    {8157} normal block at 0x036FE008, 8 bytes long.
    Data: < > 00 00 00 00 FF FF CD CD
    {8156} normal block at 0x036FDFB0, 24 bytes long.
    Data: < o > C9 90 C1 CD CE CD CD CD 01 00 00 C0 08 E0 6F 03
    {8155} normal block at 0x036FDF68, 8 bytes long.
    Data: < > 00 00 00 00 FF FF CD CD
    {8154} normal block at 0x036FDF10, 24 bytes long.
    Data: <, h o > 2C 91 C1 CD CE CD CD CD 01 00 00 C0 68 DF 6F 03
    {8153} normal block at 0x036FDEC8, 8 bytes long.
    Data: < > 00 00 00 00 FF FF CD CD
    {8152} normal block at 0x036FDE70, 24 bytes long.
    Data: < o > C8 90 C1 CD CE CD CD CD 01 00 00 C0 C8 DE 6F 03
    {8151} normal block at 0x036FDE10, 32 bytes long.
    Data: <p o o o P o > 70 DE 6F 03 B0 DF 6F 03 10 DF 6F 03 50 E0 6F 03
    {8150} normal block at 0x036FDDC8, 12 bytes long.
    Data: < g o > C8 0D 18 67 04 00 00 00 10 DE 6F 03
    {8149} normal block at 0x036FDD68, 32 bytes long.
    Data: < o > C8 DD 6F 03 CD CD CD CD CD CD CD CD CD CD CD CD
    {8148} normal block at 0x036FDCE8, 64 bytes long.
    Data: < o D g > CC CD CD CD F0 DB 6F 03 44 C2 18 67 01 00 00 00
    {8144} normal block at 0x036FDBF0, 70 bytes long.
    Data: < > 02 00 00 00 0D 00 00 00 1B 00 00 00 10 00 00 00
    {8135} normal block at 0x036FD280, 12 bytes long.
    Data: <p o o > 70 82 6F 03 AD 17 05 E4 F8 D6 6F 03
    {8134} normal block at 0x036FDBA8, 8 bytes long.

    The dump is very long and there is no links to files in it so this leaks is not in my test app.



  • Oh, god. I can't repeat this situation in WindowsXP. Can some one try to repeat this in Windows 7 32bit? My screenshot was made in Windows7 64bit.



  • [quote author="anaksimandr" date="1355079987"]What are you talking about? I push the button only one time and the widgets creates only one time so there is no leak in it. Do not waste my time with empty speculation. I don't want to talk about it more.

    I don't open a bug ticket because memory freed when I close the widget where the buttons are so it's strange (not simple leak). Dragging mouse over the buttons eats memory but the memory frees when the buttons destroys.
    [/quote]

    Hi,
    please be patient. There are real memory leaks in your code, regardless if you click the button once or more often. That was just pointed out.

    The thing you describe is something like:

    My app is running and hovering the mouse over buttons increases the memory usage in the process explorer. When I close the window, the memory goes away. Right?

    This is IMHO no memory leak, it's memory usage. Hovering over controls perhaps creates new images for displaying them or other stuff. A leak is typically not anymore referenced memory, which means it will never be freed again.



  • bq. Hi,
    please be patient. There are real memory leaks in your code, regardless if you click the button once or more often. That was just pointed out.
    The thing you describe is something like:
    My app is running and hovering the mouse over buttons increases the memory usage in the process explorer. When I close the window, the memory goes away. Right?
    This is IMHO no memory leak, it’s memory usage. Hovering over controls perhaps creates new images for displaying th

    You are right in all, but how such simple app can use over 100mb memory? This is my question. More moves more memory. Why?

    Here was found that problem is in windows7 style.
    http://stackoverflow.com/questions/13796842/memory-leaks-in-qpushbutton-or-what


  • Moderators

    If you are sure there is a memory leak, then "please file a bug report":https://bugreports.qt-project.org/ . Mentioning issues in random places on the internet (stackoverflow or here) will not get anything fixed.



  • It looks to me its a bug in QT5.
    Hovering is not defined anywhere and those few not saved references can't cause incremental memory usage.




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.