From 403a7ff3917d8d0453b68d9859393be2288e877b Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 26 Jan 2012 08:37:24 +0000 Subject: [PATCH] fixed memory leak in cap_libv4l.cpp (ticket #1518; thanks to gorgen for the patch) --- modules/highgui/src/cap_libv4l.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/highgui/src/cap_libv4l.cpp b/modules/highgui/src/cap_libv4l.cpp index ce75072..971793d 100644 --- a/modules/highgui/src/cap_libv4l.cpp +++ b/modules/highgui/src/cap_libv4l.cpp @@ -821,8 +821,13 @@ static int _capture_V4L2 (CvCaptureCAM_V4L *capture, char *deviceName) #ifdef USE_TEMP_BUFFER if (n_buffers == 0) { - capture->buffers[MAX_V4L_BUFFERS].start = malloc(buf.length); - capture->buffers[MAX_V4L_BUFFERS].length = buf.length; + if (capture->buffers[MAX_V4L_BUFFERS].start) { + free(capture->buffers[MAX_V4L_BUFFERS].start); + capture->buffers[MAX_V4L_BUFFERS].start = NULL; + } + + capture->buffers[MAX_V4L_BUFFERS].start = malloc(buf.length); + capture->buffers[MAX_V4L_BUFFERS].length = buf.length; }; #endif } @@ -1006,6 +1011,10 @@ static CvCaptureCAM_V4L * icvCaptureFromCAM_V4L (int index) capture->width = DEFAULT_V4L_WIDTH; capture->height = DEFAULT_V4L_HEIGHT; +#ifdef USE_TEMP_BUFFER + capture->buffers[MAX_V4L_BUFFERS].start = NULL; +#endif + /* Select camera, or rather, V4L video source */ if (index<0) { // Asking for the first device available for (; autoindexFirstCapture = 0; } @@ -1694,6 +1702,13 @@ static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture ){ if (capture->frame.imageData) cvFree(&capture->frame.imageData); +#ifdef USE_TEMP_BUFFER + if (capture->buffers[MAX_V4L_BUFFERS].start) { + free(capture->buffers[MAX_V4L_BUFFERS].start); + capture->buffers[MAX_V4L_BUFFERS].start = NULL; + } +#endif + //v4l2_free_ranges(capture); //cvFree((void **)capture); } -- 2.7.4