In an OpenCV application, how do I identify the so

2019-02-10 12:42发布

I have a memory leak in my OpenCV application. Its a medium size application with a dozon of classes and a few thousands lines of code. Somehow, I managed to produce a large memory leak in my application that it eats away all of my 8gb memory in a few minutes. I am using OpenCV C++ 2.3 on Ubuntu 11.10 with CMake.

An snapshot of how much memory is freed right after I terminate the app. I can watch the used memory go up to 4gig in a matter of a few minutes

It is a hand tracking application and it process two video streams simultaneusly at a frame rate of around 15fps for each camera.

I tried using valgrind like below, but the output of valgrind is so huge that exceeds the amount of text shell can keep in buffer. I know I can save the output to a log file, but I was hoping to avoid the daunting task of reading through all of it. Here is the valgrind command I used:

valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./Gibbon 

Here is the last few lines of valgrind output:

==3573== 5,415,576 (1,176 direct, 5,414,400 indirect) bytes in 7 blocks are definitely lost in loss record 2,571 of 2,571
==3573==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==3573==    by 0x5B2ACD0: cv::fastMalloc(unsigned long) (in /usr/local/lib/libopencv_core.so.2.3.1)
==3573==    by 0x5A7FA9D: cvCreateImageHeader (in /usr/local/lib/libopencv_core.so.2.3.1)
==3573==    by 0x484538: CameraPGR::convertImageToOpenCV(FlyCapture2::Image*) (CameraPGR.cpp:212)
==3573==    by 0x483F52: CameraPGR::grabImage() (CameraPGR.cpp:134)
==3573==    by 0x473F86: start() (GibbonMain.cpp:368)
==3573==    by 0x4725CC: main (GibbonMain.cpp:108)
==3573== 
==3573== LEAK SUMMARY:
==3573==    definitely lost: 24,432 bytes in 33 blocks
==3573==    indirectly lost: 5,414,640 bytes in 15 blocks
==3573==      possibly lost: 2,314,837 bytes in 1,148 blocks
==3573==    still reachable: 496,811 bytes in 4,037 blocks
==3573==         suppressed: 0 bytes in 0 blocks
==3573== 
==3573== For counts of detected and suppressed errors, rerun with: -v
==3573== Use --track-origins=yes to see where uninitialised values come from
==3573== ERROR SUMMARY: 336 errors from 318 contexts (suppressed: 10 from 8)

What are some better ways that I can approach this problem? Are there some tools that can show me in a concise way what function calls are causing most of the memory allocations? If valgrind is the answer, I would appreciate some hints on how to use it in a more efficient way since I am totally new to this tool.

1条回答
劫难
2楼-- · 2019-02-10 13:04

Not an answer, but a suggestion: Move from OpenCV C interface to C++. If properly used, it will minimize your chances for a leak, now and in the future. Its smart pointers embedded in the objects automatically free memory.

In the worst case, you'll have a performance penalty (too many allocs/deallocs), but those are easy to spot in a profiler.

The C++ interface is using

Mat intead of IplImage, 
Point instead of CvPoint, 
cv::function() instead of cvFunction. 

And you do not have to declare pointers to images:

Mat src = imread("myfile.jpg");
Mat gray; // note that I do not allocate it. 
// This is done automatically in the next functions
cv::cvtColor(src, gray, CV_BGR2GRAY);
imshow("Gray image", gray);
waitKey();

If you have some legacy code, or a third-party that uses the other interface, it's easy to convert back and forth:

Mat src(width, height, CV_8UC3);
IplImage* legacyImg;
legacyImg = &(IplImage)src;

Other datatypes (like CvPoint) are automatically converted. CvSeq is replaced by std::vector<T>

查看更多
登录 后发表回答