Valgrind Reports Possible Memory Leak using OpenCV C++ Libraries

asked 2014-10-29 14:26:10 -0500

kevin.gurney gravatar image

updated 2014-10-30 08:45:45 -0500

I have created a very simple program, which essentially does nothing in particular other than include the opencv2/core/core.hpp header.

However, when I run the program using Valgrind it always reports "possibly lost: 4,676 bytes in 83 blocks" seeming to suggest that there may be a memory leak in my program or the OpenCV C++ bindings.

#include <opencv2/core/core.hpp>

int main() {
    return 0;

The output from Valgrind is:

==30842== Memcheck, a memory error detector
==30842== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==30842== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==30842== Command: ./opencv-testing
==30842== HEAP SUMMARY:
==30842==     in use at exit: 307,778 bytes in 1,401 blocks
==30842==   total heap usage: 2,628 allocs, 1,227 frees, 400,413 bytes allocated
==30842== LEAK SUMMARY:
==30842==    definitely lost: 0 bytes in 0 blocks
==30842==    indirectly lost: 0 bytes in 0 blocks
==30842==    possibly lost: 4,676 bytes in 83 blocks
==30842==    still reachable: 303,102 bytes in 1,318 blocks
==30842==         suppressed: 0 bytes in 0 blocks
==30842== Rerun with --leak-check=full to see details of leaked memory
==30842== For counts of detected and suppressed errors, rerun with: -v
==30842== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Is OpenCV really leaking memory, or is Valgrind just misinterpreting the memory management system that OpenCV uses? Is there a way to prevent these "possibly lost" memory errors from occurring when using OpenCV?

edit retag flag offensive close merge delete


Rerun with --leak-check=full to see details of leaked memory can you do that and see where exactly openCV is leaking memory? And actually, the possible losses are probably no losses at all. Your program isn't even addressing OpenCV processing/code!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-30 08:51:58 -0500 )edit

I assumed that the possible losses are probably not actually losses at all, but it seems a bit strange that there are even "possible" leaks being reported by Valgrind. The output from running Valgrind with the --leak-check=full flag can be viewed here:

kevin.gurney gravatar imagekevin.gurney ( 2014-10-30 10:40:39 -0500 )edit

It is not particularly apparent to me where the possible leaks are occurring based on the output of Valgrind.

kevin.gurney gravatar imagekevin.gurney ( 2014-10-30 20:46:03 -0500 )edit

The report shows that the leaks occur from native linux libraries and NOT from openCV.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-31 04:27:47 -0500 )edit

OK, thank you for clarifying! I apologize for the confusion. This may be beyond the scope of this question, but is there any obvious reason that I am missing as to why the native Linux libraries would be leaking memory?

kevin.gurney gravatar imagekevin.gurney ( 2014-10-31 10:02:56 -0500 )edit