How to solve QPixmap::fromImage memory leak?
There is no way to create QPixmap
directly from IplImage*
(and also not from cv::Mat
). However, your solution there is to refrain from writing everything into one line and work with a pointer that you free yourself.
e.g.:
if (this->mypixmap)
delete this->mypixmap;
this->mypixmap = new QPixmap(QPixmap::fromImage(tmp).scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
this->setPixmap(this->mypixmap);
Don't forget to set mypixmap(NULL)
in the constructor.
DoDo
Updated on June 04, 2022Comments
-
DoDo almost 2 years
I have a problem with Qt.
Here is a part of code that troubles me:
void FullScreenImage::QImageIplImageCvt(IplImage *input) { help=cvCreateImage(cvGetSize(input), input->depth, input->nChannels); cvCvtColor(input, help, CV_BGR2RGB); QImage tmp((uchar *)help->imageData, help->width, help->height, help->widthStep, QImage::Format_RGB888); this->setPixmap(QPixmap::fromImage(tmp).scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); cvReleaseImage(&help); } void FullScreenImage::hideOnScreen() { this->hide(); this->clear(); } void FullScreenImage::showOnScreen(IplImage *slika, int delay) { QImageIplImageCvt(slika); this->showFullScreen(); if(delay>0) QTimer::singleShot(delay*1000, this, SLOT(hideOnScreen())); }
So, the method
showOnScreen
uses private methodQImageIplImageCvt
to createQImage
fromIplImage
(which is used by the OpenCV), which is then used to createQPixmap
in order to show the image in full screen.FullScreenImage
class inheritsQLabel
.After some delay, the fullscreen picture should be hidden, so I use
QTimer
to trigger an event after some delay. The event handler is thehideOnScreen
method which hides the label and should clear the memory.The problem is the following:
Whenever I call
QPixmap::fromImage
, it allocates the memory for the pixmap data and copies the data fromQImage
memory buffer to theQPixmap
memory buffer. After the label is hidden, theQPixmap
data still remains allocated, and even worse, after the newQPixmap::fromImage
call the new chunk of memory is allocated for the new picture, and the old data is not freed from memory. This causes a memory leak (cca 10 MB per method call with my testing pictures). How can I solve that leak?I've even tried to create a private
QPixmap
variable, store pixmap created by theQPixmap::fromImage
to it, and then tried to call its destructor inhideOnScreen
method, but it didn't help.Is there a non-static way to create
QPixmap
fromQImage
? Or even better, is there a way to createQPixmap
directly fromIplImage*
?Thank you in advance for your answers.