Important: Please read the Qt Code of Conduct -

QPushButton::setIcon produces memory leaks

  • Hi all,

    I have found a possible bug.
    When using the QPushButton::setIcon() function multiple times the memory is increasing with Qt 5.8 on Windows 10.
    I checked some cases with my little test program here. To see the effect I called the slot multiple times with a timer.

    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QTimer* timer = new QTimer( this );
        connect( timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
        timer->start( 10 );
    void MainWindow::onTimeout()
        ui->pushButton->setIcon( QIcon( ":/images/icon.png" ) );

    I analyzed some cases:

    This is working with Qt 4.8 but not with 5.8:
    With Qt 5.8 memory is increasing with 200 kb/s.

    ui->pushButton->setIcon( QIcon( ":/images/icon.png" ) );

    This is working on Qt 4.8 and 5.8 (no memory leaks):

    ui->pushButton->setIcon( QIcon( QPixmap( ":/images/icon.png" ) ) );

    I used Qt 4.8 with msvc2008.
    I used Qt 5.8 with msvc2015 and mingw. Both same behavior.
    Can someone confirm this?


  • Lifetime Qt Champion

    Can you link to the complete test project ?
    So we use same icons etc.

  • I test the code "ui->pushButton->setIcon(QIcon(":/backword.png"));" in the timeout slot,and timer start by 10ms,it is ok.The memory is ok.

  • Lifetime Qt Champion

    Win 7, mingw Qt5.7.1
    Also see 200 kb increase. pr tick.
    But it never went over 40k. even running for long time.
    So not leak in 5.7

    //your code here

  • OK, no leak. I have also recognized this in further tests. And if you stop the timer after 10000 ticks you can see that the memory is somehow decreasing. Perhaps the garbage collector is doing his job with a loooong delay.

Log in to reply