简体   繁体   中英

QT live camera on QImage. Application crashes due to out of memory. Memory increases with speed

I am using QT Qpixmap to show the image captured from the camera using the OpenCV frame. I am doing following steps

Capture image using OpenCV

Convert the OpenCV image to QImage

Convert QImage to Qpixmap

Show it on Qlabel

The problem I am facing is the memory start increasing at great speed and after few time the application crashed with out of memory

I have gone through the code several time to check any object being created again and again. The problem is related to working inside a thread.

Sample code is as follow

void mainfucntion(){
std::thread producer_t(&MainWindow::RunDefaultCamera,this);

for(;;){

        time(&start);
        timer = double(getTickCount());
        tic();

        if(!bufferQueue.empty()){
          lock_guard<std::mutex> lock(fmutex);
            readFrame = bufferQueue.front();
            qDebug() << "1 : " << bufferQueue.size();
            bufferQueue.pop_front();
            qDebug() << "2 : " << bufferQueue.size();
        }
        else{
            if(keepRunning == true)
            {
                if(threadEnable==false)
                {
                   std::thread producer_t(&MainWindow::RunDefaultCamera,this);
                }
                continue;
            }
            else{
                producer_t.join();
                return -1;
            }
        }
// cap >> readFrame;
cv::resize(readFrame, readFrame, size);

img = QImage((uchar*) readFrame.data, readFrame.cols, readFrame.rows, readFrame.step, QImage::Format_BGR888);
image = QPixmap::fromImage(img);
img = QImage(); // releasing memory

        ui->lblDisplayVideo->setPixmap(image);
}

Thread function is here

void RunDefaultCamera()
{
while(capture.isOpened())
{

        qDebug() << "thread is running";
        capture >> ImageMat;
        bufferQueue.push_back(ImageMat);
        
        if(!ImageMat.empty())
        {
            frameCounter++;

            lock_guard<std::mutex> lock(fmutex);
            if (int(bufferQueue.size()) >= bufferSize)
            {
                bufferQueue.clear();
            }
            else
            {
                bufferQueue.push_back(ImageMat);
             }

        }
        sleep(100);
//        ui->listInfo->addItem(QString::number(bufferQueue.size()));
        qDebug() << bufferQueue.size();
}
capture.release();
}

After trying many things here is the conclusion and code is now working perfectly fine

I removed the tic toc part

time(&start);
timer = double(getTickCount());
tic();

It was working but crashing then just to make sure that QImage is not NULL I removed the img = QImage(); // releasing memory img = QImage(); // releasing memory

with

if(!img.isNull())
    img = QImage();

Its working perfectly fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM