Memory leak on webcam capture [closed]

asked 2018-05-31 07:44:14 -0500

vpartel gravatar image

updated 2018-05-31 12:54:16 -0500

Hello all, I am running a code in linux, C, part of a deep learning (YOLO) project and I am having problems with a memory leak omewhere related to opencv.

I am using 3 webcams in my project at the same time and placing the captures together side by side using the function:

cap = get_capture_webcam(1); cap2 = get_capture_webcam(2); cap3 = get_capture_webcam(3);

There is some memory leak in the code because after about 2 min it crashes and I have to restart it. I've tried releasing every image I use and I still can't find the leak.

Does anyone know common causes of leaks using this function or any other that I might be using?

Thanks in advance

Edit: Okay sorry. So this is the function that is causing the leak (I'm pretty sure)

int fill_image_from_stream2(CvCapture *cap, CvCapture *capi2, CvCapture *capi3, image im)
    IplImage* src = cvQueryFrame(cap);
 if (!src) return 0;
int w = src->width;
int h = src->height;

IplImage* src2 = cvQueryFrame(capi2);
if (!src2) return 0;
IplImage* src3 = cvQueryFrame(capi3);
if (!src3) return 0;
IplImage *imagem = cvCreateImage(cvSize(w*3,3*h/4), src->depth, src->nChannels);

cvSetImageROI(imagem, cvRect(0, 0, w, h));
cvSetImageROI(src, cvRect(0, h/4, w, 3*h/4));
    cvCopy(src,imagem, NULL);
cvSetImageROI(imagem, cvRect(w, 0, src2->width, src2->height));
cvSetImageROI(src2, cvRect(0, h/4, w, 3*h/4));
    cvCopy(src2,imagem, NULL);
cvSetImageROI(imagem, cvRect(w*2, 0, src3->width, src3->height));
cvSetImageROI(src3, cvRect(0, h/4, w, 3*h/4));
    cvCopy(src3,imagem, NULL);
//image im2 = ipl_to_image(imagem);

    ipl_into_image(imagem, im);
imagem = 0;
src = 0;
src2 = 0;
src3 = 0;
    return 1;

It crashes after about 2000 frames.

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by berak
close date 2018-05-31 13:06:46.897529


that's not enough code, to help. get_capture_webcam() ? show your processing !

and, even though darknet is a C library , you should use opencv's c++ api for anything else.

berak gravatar imageberak ( 2018-05-31 07:55:30 -0500 )edit

Sorry and thank you, I've edited the question

vpartel gravatar imagevpartel ( 2018-05-31 12:54:41 -0500 )edit

your memleak starts with cvCreateImage

berak gravatar imageberak ( 2018-05-31 13:04:25 -0500 )edit

opencv went to using c++ (cv::Mat) in 2010 already (deprecating that ill-fated c-api).

you're living under rock

berak gravatar imageberak ( 2018-05-31 13:07:59 -0500 )edit

The code is in C already, I can't start my own code from scratch. What am I supposed to do?

vpartel gravatar imagevpartel ( 2018-05-31 14:23:22 -0500 )edit