Solved QImage load images return 0 occasionlly
-
I load image with QImage,then scaled,create a QPixmap with that QImage,finally set it to a QLabel to show.
At the begin, it works well. But sometimes it failed at loading the same image that was successfully loaded before. After that, whatever image loaded, it always return 0.
Has anyone come to this question before?
Here is my code :
void show_image(const QString& imagePath) {
QImage image; int ret = image.load(imagePath); LOG(DEBUG) << "Load image" << imagePath.toStdString() << ": " << ret; if (ret) { QPixmap pixmap = QPixmap::fromImage(image.scaled(170, 170, Qt::IgnoreAspectRatio)); if (!pixmap.isNull()) { imageLabel->setPixmap(pixmap); } }
}
Consider I looped call show_image with a collection of images.
-
Hi
Did you check if you run out of memory ?
It could sound like that. -
@mrjj Thank you for replying. I checked days ago, it's very likely a memory leak problem. But the code i posted seemed has no relationship with the leak, i will try to find the leak somewhere else in my code.
-
@huhansan
Hi
Ok. I would go to look for widgets that you new and give no parent.
Or places where you give the images as pointers. -
I has one widget that does not has one parent. It is usually invisible。 When I need to show an image, i set this widget visible, after serval seconds, set it back to invisible again. After hundreds of this kind of procedures, QImage.load() return 0.
I watched the memory's usage , it's always growing. Has the step i set the widget visible/invisible has some influence on the memory usage?
-
@huhansan said in QImage load images return 0 occasionlly:
Has the step i set the widget visible/invisible has some influence on the memory usage?
Not really, but how you're loading and keeping the image does. You will need to show more code (how exactly do you load the image in that widget?).
-
Thank you for replying.
I customered one window inherted from QMainWindow which also has a QLabel to show image i setted.
This window is usually invisible, when i load an image and call the function QLabel.setPixmap, i set it visible.
Servals seconds set the window to invisible, and go on.
The code used to load image is
QImage image; int ret = image.load(imagePath); // After hundreds of loading actions, image.load always return 0 LOG(DEBUG) << "Load image" << imagePath.toStdString() << ": " << ret; if (ret) { QPixmap pixmap = QPixmap::fromImage(image.scaled(170, 170, Qt::IgnoreAspectRatio)); if (!pixmap.isNull()) { // If pimmap is not now, set to QLabel to show imageLabel->setPixmap(pixmap); } } // Set the window visible setVisible(true); }
I just googled setVisible(bool) function. It says the window object was created when set the window to Visible. So i was thinking , is it the memory was leaked every time i set the window to visible, then to invisible because of the window has no parents?
-
@huhansan This looks good. You could use Valgrind (Linux/UNIX tool) to check for memory leaks.
-