JPery / MJPEGWriter

Lightweight HTTP server to stream your OpenCV processing in C++
MIT License
98 stars 40 forks source link

Something here seems to be hoarding memory #9

Closed dnicholes closed 4 years ago

dnicholes commented 5 years ago

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 5 years ago

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

Best regards, Jorge

dnicholes commented 5 years ago

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 5 years ago

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

dnicholes commented 5 years ago

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 5 years ago

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