Important: Please read the Qt Code of Conduct -

Memory leak?

  • Hello
    If I set a pixmap to a QLabel and the set another in the following fashion:


    Will this release all the memory or cause a memory leak? Also, is this the correct way och swapping images? and is there any downside? I'd like to avoid creating image pointers and reserving memory for the pixmaps with new, due to the considerable amount of "on/off" images.

  • Looking at the declaration of "QLabel::setPixmap": - it takes a constant reference to a QPixmap and looking at the definition, noticed that there is a QPixmap object is created with new and assigned to local pointer.

    Before creating the new QPixmap, contents are cleared as well.

    Hence, I think there wont be any memory leak, as memory management is properly done.

  • QPixmap is implicitly shared, thus I suggest to create the pixmap once [probably as a private (static) member of your class] and to pass the reference as required.

  • Since imglabel will be the owner of the QPixmap it will release all memory used. So no memory leak here. Qt is pretty strong in memory management.

Log in to reply