Home OpenCV leaks memory (cvResize, cvCreateImage)
Reply: 1

OpenCV leaks memory (cvResize, cvCreateImage)

Panni
1#
Panni Published in 2017-12-06 21:29:49Z

I'm running OpenCV on a RaspberryPi and use OpenCVs C interface.

I need to resize the dimensions of an image from a webcam, therefore I used the cvResize() function.

It works fine but after a few seconds I run out of memory, since I'm executing the code inside a while(1)-Loop and read about other people having this problem, I suspect a memory leak.

Here is my code:

IplImage *frame;
IplImage *frameRaw;

main() {
    CvCapture *capture = cvCreateCameraCapture(-1);

    while (1) {
        frameRaw = cvQueryFrame(capture);
        frame = cvCreateImage(cvSize(WIDTH, HEIGHT), frameRaw->depth, frameRaw->nChannels);

        cvResize(frameRaw, frame, 0); // 0 = CV_INTER_NEAREST

        // Do something with "frame"
    }

}

I already tried to free the reserved memory at the end of each iteration using cvReleaseImage(&frameRaw) (or &frame) but that always caused a segmentation fault. Using cvReleaseImageHeader() caused no segmentation fault but also didn't free any memory.

Also I tried to change the capture-resolution of the frames via cvSetCaptureProperty() but that did nothing.

Can someone help me understand what's going wrong here?

Thanks in advance

Dan Mašek
2#
Dan Mašek Reply to 2017-12-06 22:09:40Z

Thanks to @DanMašek I found the solution:

After each while-loop iteration the allocated memory for the frame has to be freed. That can be accomplished using cvReleaseImage(&frame).

The complete code now looks like this:

IplImage *frame;
IplImage *frameRaw;

main() {
    CvCapture *capture = cvCreateCameraCapture(-1);

    while (1) {
        frameRaw = cvQueryFrame(capture);
        frame = cvCreateImage(cvSize(WIDTH, HEIGHT), frameRaw->depth, frameRaw->nChannels);

        cvResize(frameRaw, frame, 0); // 0 = CV_INTER_NEAREST

        // Do something with "frame"

        cvReleaseImage(&frame);
    }

}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300012 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO