Memory leak issue
-
Hi all,
I'm facing some memory leak issue, i have a tabwidget, there are 4 tabs each tab contains video player(QLabel) and i am fetching video in a different thread and emitting a pixmap from there. once the signal emitted from the thread, setting that pixmap to the active tab video player. the problem is if i run the application memory keep on increases and once and once system disc will be full application gets crashed. please help me with appropriate solution.
please refer the code bellow:
//tab widget
void ServerSetupWidget::SLT_RecieveImage(QPixmap *img)
{
if(m_Image!=NULL){
delete m_Image;
}
m_Image = img;
QRect rec = QApplication::desktop()->screenGeometry();
int height = rec.height();
int width = rec.width();if(m_stop||m_pause ){ if(m_stop){ m_VideoPlayer->setPixmap(QPixmap(":/Images/radioBack.jpg").scaled(width/2,height/2)); } return; } if(!m_Image->isNull() ){ //qDebug()<<Q_FUNC_INFO<<"In========"<<endl; if(m_play && m_isVideoSelected){ m_VideoPlayer->setPixmap(m_Image->scaled(width/2,height/2)); } }else{ } this->update();
}
//Imageprovider
void FrameProvider::processFrames()
{
VideoCapture cap(m_VideoPath);if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return ;
}while(1)
{
Mat frame;
cap >> frame;QPixmap *Pixmap = new QPixmap(QPixmap::fromImage(QImage((unsigned char*) frame.data, frame.cols, frame.rows, QImage::Format_RGB888))); emit SGL_transferPix(Pixmap); this->msleep(15);
}
cap.release();
destroyAllWindows();
} -
@Venkatesh-V QPixmap must only be created or used in the GUI thread (that means the thread which created
QApplication
). You must not use QPixmap in other threads.However, you can use QImage in other threads.
Also, QPixmap is implicitly shared. Don't use
new
, just create a QPixmap on the stack and pass by-value or by-const-reference.