I want to show a image on qt label. I am getting image data in the form of QByteArray
and I am loading it into label.
Below is the code :
defined in constructor
QPixmap *pixmapTest;
pixmapTest = NULL;
the following code is in a function which is getting called multiple times :
RequestCompleted(QNetworkReply *reply)
{
if(pixmapTest){
qDebug()<<"delete showImage Pixmap Object";
delete pixmapTest;
pixmapTest = NULL;
}
pixmapTest = new QPixmap();
QByteArray jpegData = reply->readAll();
pixmapTest->loadFromData(jpegData);
ui.qtLabel->setPixmap(*pixmapTest);
}
After calling this function for around 500 times I am getting this error
QImage: out of memory, returning null image.
I am not getting what is the error in the above code. Can someone please tell me how to solve this?
First off allocating and de-allocating memory for a variable (
pixmapTest
) in a function that gets called many times doesn't make sense enough. You should allocate the memory first and once all is done de-allocate it. For example:Apart from this, it sounds like your app is leaking memory. Note that by deleting the
pixmapTest
, you're not deleteing the memoryjpegData
points to. Therefore, in each function call you should take care of the memory to whichjpegData
points.