OpenCV 3.1 videoCapture memory leak on ubuntu 16.04 detected by Valgrind

asked 2017-03-09 11:14:33 -0500

fanyuan gravatar image

updated 2017-03-09 11:18:08 -0500

Problem: memory leak detected by valgrind with using videoCapture func in OpenCV

OpenCV version: 3.1.0 OS: Ubuntu 16.04 Valgrind version: 3.11.0

The program I used for testing is from the OpenCV official Website.

Here is the code:

#include "opencv2/opencv.hpp" using namespace cv; int main(int, char**) {
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;
    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, COLOR_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0; }

There is no problem in executing the program, but lots of memory leak detected by valgrind, here is the log summary:

  ==30699== LEAK SUMMARY:
    ==30699==    definitely lost: 832 bytes in 2 blocks
    ==30699==    indirectly lost: 0 bytes in 0 blocks
    ==30699==      possibly lost: 929,414 bytes in 76 blocks
    ==30699==    still reachable: 1,878,713 bytes in 16,617 blocks
    ==30699==                       of which reachable via heuristic:
    ==30699==                         length64           : 5,376 bytes in 87 blocks
    ==30699==                         newarray           : 5,192 bytes in 54 blocks
    ==30699==         suppressed: 0 bytes in 0 blocks
    ==30699== Reachable blocks (those to which a pointer was found) are not shown.
    ==30699== To see them, rerun with: --leak-check=full --show-leak-kinds=all
    ==30699== 
    ==30699== For counts of detected and suppressed errors, rerun with: -v
    ==30699== ERROR SUMMARY: 46 errors from 46 contexts (suppressed: 0 from 0)

How can I solve the memory leak problem? Any comments are appreciated.

Here is the detailed log info.

==30699== Memcheck, a memory error detector
==30699== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==30699== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==30699== Command: ./hzTestFeatureProcMain
==30699== Parent PID: 8643
==30699== 
==30699== 
==30699== HEAP SUMMARY:
==30699==     in use at exit: 2,906,167 bytes in 17,486 blocks
==30699==   total heap usage: 102,435 allocs, 84,949 frees, 124,790,428 bytes allocated
==30699== 
==30699== 16 bytes in 1 blocks are possibly lost in loss record 1,455 of 7,415
==30699==    at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30699==    by 0x9742770: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2)
==30699==    by 0x94CD522: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.2)
==30699==    by 0x94D1F90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.2)
==30699==    by 0x94B130B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.2)
==30699==    by 0x94AB177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.2)
==30699==    by 0x40104E9: call_init.part.0 (dl-init.c:72)
==30699==    by 0x40105FA: call_init (dl-init.c:30)
==30699==    by 0x40105FA: _dl_init (dl-init.c:120)
==30699==    by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==30699== 
==30699== 16 bytes in 1 blocks are possibly lost in loss record 1,456 of 7,415
==30699==    at 0x4C2DB8F: malloc (in /usr ...
(more)
edit retag flag offensive close merge delete

Comments

Can you try with OpenCV 3.2. You should also test the different part for memory leak: for instance a program with only one cvtColor(), then add GaussianBlur(), etc.

Eduardo gravatar imageEduardo ( 2017-03-10 05:03:55 -0500 )edit