JPery/MJPEGWriter

Something here seems to be hoarding memory

Closed this issue · 5 comments

I have incorporated this into a C++ design on ARM. I instantiate MJPEGWriter in the class in another classes constructor like this:

mjpgWriter = new MJPEGWriter(8080);

Then to write a new frame I use this code:

mjpgWriter->write(drawing); if(!mjpgWriter->isOpened()) { mjpgWriter->start(); }

Over the course of about a day MJPGWriter goes from consuming very little to all of the system memory (4GB in this case) and crashes.

Any idea why this might be happening?

JPery commented

Are you releasing the "drawing" variable? If you're updating it but not releasing it a memory leak may happen.

Best regards, Jorge

Thank you so much for replying. This is really helpful. I do release the
frame after passing it to the writer. Here is a few more lines surrounding
the call to write():

        Mat dispMat = drawing.clone();

        // Send to server
        mjpgWriter->write(dispMat);
        if(!mjpgWriter->isOpened()) {
            qInfo() << "Opening MJPG Writer Socket";
            mjpgWriter->start();
        }
        dispMat.release();
JPery commented

In your code you have to also call drawing.release() in order to free this variable.

Also, the library already does drawing.clone() when writing a frame to it.

Hope this helps

Thank you again for the reply. Memory hoarding continues even with the change to the code. Here is an even broader view of what I am doing:

void FrameProcessor::updateRawFrame(uchar *procBuf) {
    
    Mat drawing(imgHeight, imgWidth, CV_8UC1);
    drawing = cv::Mat(imgHeight, imgWidth, CV_8UC1, rawBuf).clone();

    // Create color image for display
    cvtColor(drawing, drawing, COLOR_GRAY2BGR);

    // Write current FPS and timedate on drawing frame
    addFpsAndTimedate(drawing);

    // Draw center marker
    drawMarker(drawing, target, CV_RGB(0, 255, 0), MARKER_DIAMOND, 10);

    // Draw box around currently tracked object
    // Draw line to object
    drawTrackInfo(drawing);

    // Send to server
    mjpgWriter->write(drawing);
    if(!mjpgWriter->isOpened()) {
        qInfo() << "Opening MJPG Writer Socket";
        mjpgWriter->start();
    }
    drawing.release();
}
JPery commented

I have not been able to replicate the issue because I don't have any ARM device to test. Could you try the sample code to see if the memory leak is part of the library? Thank you