Nominate our 2022 Qt Champions!

Constantly generating QPixmap* fails after a number of iterations

  • I am using the following code in order to generate QPixmap* pointers and then insert them into QHash<QString, QPixmap*> (I will show only the pointers generation code since this is the one that fails).

    QPixmap* MyClass::loadImg(QString fileName)
         QImage qimage(fileName);
         if (qimage.isNull()) {
             qDebug() << "Cannot load image " <<  fileName;
         QPixmap *image = new QPixmap(fileName);
         return image;

    The problem that I have is the following: For the first about 200 calls the method works fine - it is being called on a loop that iterates through the image files of a directory. Then suddenly the QPixmap* starts returning QPixmap(null) for no apparent reason.QImage is also null when that happens. I have checked and made sure that the path is fine. Also, I have tried with various sets of images and the same always happens - it runs with no problems the ~200 calls and then starts generating nulls.

    Any help would be appreciated.

    Thank you.

    P.S. I have also posted the question on stackoverflow:

  • Now that I run it again, QImage is not null. Only the QPixMap* image is null.
    However I still cannot find what is wrong.

  • Someone replied on the stackoverflow and told me to use non-pointer variables for the QPixmaps that I generate. This has now fixed my problem. Thank you.

  • Just don't create it on heap. QPixmap is implicitly shared.

    It does not matter how you create pixmap. What does is total number of allocated resources.
    Changes to your code might fix something if your previous code did not clean the memory -did not delete pixmap.
    So code causing problem was not posted.

Log in to reply