3

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.

Soc
  • 283
  • 3
  • 17

1 Answers1

3

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

RazrFalcon
  • 787
  • 7
  • 17
  • This seems to have solved the problem so now I am using a `QHash` and have changed the code to generate non pointer variables. Thank you for you help. Just out of interest, why does it not work with pointers ? – Soc Sep 02 '15 at 17:14
  • 1
    @Soc Since you stored raw pointers, you likely either leaked them like a sieve and you ran out of memory/resources, or you did double deletions and corrupted the state of your program, etc. I'm pretty sure that it'd all work fine had you used a shared smart pointer to a pixmap, like `std::shared_ptr`, or `QSharedPointer` - even if completely unnecessary given the implicit sharing of the value types in question (`QPixmap` etc.). – Kuba hasn't forgotten Monica Sep 02 '15 at 17:20
  • @KubaOber Thank you for the info. I tried to use `QPointer` but it does not work with `QPixmap` since it is not a subclass of `QObject`. From now on I will keep in mind the `QSharedPointer` but in this instance I will just use the solution from above since it works fine. – Soc Sep 02 '15 at 17:28
  • @Soc I didn't tell you to use `QPointer`, it's not an owning pointer anyway so its use would make no sense. I'm simply stating that the source of your trouble wasn't heap allocation, but resource leaks or invocation of undefined behavior due to double deletion. The pointers are unnecessary, but I can run your very code on a million (10^6) images and it works fine. Even with raw pointers, as long as I use them correctly. The problems are elsewhere in the code you don't show and nothing convinces me you have fully solved them either. – Kuba hasn't forgotten Monica Sep 02 '15 at 21:18
  • @KubaOber I meant that I tried `QPointer` before your first reply. Now that I am not using pointers what problems might be there that I might have not spotted yet ? Thank you. – Soc Sep 03 '15 at 13:05
  • @Soc I can't tell you without seeing your code. Whatever you did wrong to misuse pointers this time is likely to exist elsewhere in your codebase... – Kuba hasn't forgotten Monica Sep 03 '15 at 13:49
  • @KubaOber I am not using pointers often and this time I wanted to do so because I did not notice that `QPixmap` is shared implicitly and so I thought that the created variable will get out of scope when the function returns. So what is the proper use of raw pointers ? Do you just have to be very carefully to always `delete` them at the end and also avoid double deletion ? Is it anything else that you need to take care of ? – Soc Sep 03 '15 at 14:00
  • 1
    @Soc The proper use of raw pointers is, ideally, not to use them to own dynamic storage. A pointer can point to a storage of any duration. If you allocated with `new`, it points to dynamic duration storage. You end the existence of such storage by `delete`-ing it. At that point, the pointer is dangling and you cannot use its present value anymore - it's up to you not to. That's why smart pointers make life much easier. – Kuba hasn't forgotten Monica Sep 03 '15 at 14:24