How to release the memory in cv::Mat

asked 2018-12-05 22:09:33 -0500

JillWang gravatar image

updated 2018-12-06 01:37:16 -0500

berak gravatar image

There are some memory leak in my project, after several round of analysis, it seems the memory in cv::Mat not be released.

My test code like this:

Mat img = imread("./a.jpg");
img.release();  //optional, both tested and got the similar result.


After running I got the mem leak msg from valgrind whether I called img.release explicitly or not:

==27271== Memcheck, a memory error detector
==27271== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==27271== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==27271== Command: ./a.out
==27271== Parent PID: 27189
==27271== 
==27271== 
==27271== HEAP SUMMARY:
==27271==     in use at exit: 72 bytes in 3 blocks
==27271==   total heap usage: 272 allocs, 269 frees, 1,467,134 bytes allocated
==27271== 
==27271== 8 bytes in 1 blocks are still reachable in loss record 1 of 3
==27271==    at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334)
==27271==    by 0x4A3B78F: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x400DB0F: call_init.part.0 (dl-init.c:72)
==27271==    by 0x400DC5F: call_init (dl-init.c:30)
==27271==    by 0x400DC5F: _dl_init (dl-init.c:120)
==27271==    by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so)
==27271== 
==27271== 8 bytes in 1 blocks are still reachable in loss record 2 of 3
==27271==    at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334)
==27271==    by 0x499DC3F: cv::Mat::getStdAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x499DC8B: cv::Mat::getDefaultAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x499E62B: cv::Mat::create(int, int const*, int) (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x4B1353F: cv::imread_(cv::String const&, int, int, cv::Mat*) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0)
==27271==    by 0x4B14457: cv::imread(cv::String const&, int) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0)
==27271==    by 0x10907F: main (main.cc:14)
==27271== 
==27271== 56 bytes in 1 blocks are still reachable in loss record 3 of 3
==27271==    at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334)
==27271==    by 0x4A3B717: cv::Mutex::Mutex() (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x4A3B797: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0)
==27271==    by 0x400DB0F: call_init.part.0 (dl-init.c:72)
==27271==    by 0x400DC5F: call_init (dl-init.c:30)
==27271==    by 0x400DC5F: _dl_init (dl-init.c:120)
==27271==    by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so)
==27271== 
==27271== LEAK SUMMARY:
==27271==    definitely lost: 0 bytes in 0 blocks
==27271==    indirectly lost: 0 bytes in 0 blocks
==27271==      possibly lost: 0 bytes in 0 blocks
==27271==    still reachable: 72 bytes in 3 blocks
==27271==         suppressed: 0 bytes in 0 blocks
==27271== 
==27271== For counts of detected and suppressed errors, rerun with: -v
==27271== ERROR SUMMARY: 0 errors from 0 contexts ...
(more)
edit retag flag offensive close merge delete

Comments

3.1.0

that's unfortunately no more relevant. could you try with a more recent 3.4 or master ?

berak gravatar imageberak ( 2018-12-06 01:39:49 -0500 )edit