在OpenCV应用程序中,如何识别内存泄漏的来源并解决它? [英] In an OpenCV application, how do I identify the source of memory leak and fix it?

查看:791
本文介绍了在OpenCV应用程序中,如何识别内存泄漏的来源并解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OpenCV应用程序中有内存泄漏。它是一个中等大小的应用程序,带有几十个类和几千行代码。不知怎的,我设法在我的应用程序中产生大量的内存泄漏,它在几分钟内消除了我的所有8gb内存。我在Ubuntu 11.10上用CMake使用OpenCV C ++ 2.3。

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.

它是一个手跟踪应用程序,它同时处理两个视频流,每帧大约15fps的帧速率。

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

我尝试使用valgrind如下,但输出valgrind是如此巨大,超过文本shell可以保存在缓冲区的量。我知道我可以保存输出到日志文件,但我希望避免阅读通过它的艰巨的任务。这是我使用的valgrind命令:

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 

这是valgrind输出的最后几行:

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)

这个问题?是否有一些工具可以以简明的方式告诉我什么函数调用导致大部分内存分配?如果valgrind是答案,我会感激一些关于如何更有效地使用它的提示,因为我是全新的这个工具。

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.

推荐答案

不是一个答案,但建议:从OpenCV C接口移动到C ++。如果正确使用,它将最大限度地减少泄漏的机会,现在和未来。其智能指针嵌入在对象中自动释放内存。

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.

在最坏的情况下,你会有一个性能损失(太多的allocs / deallocs),但是这些都很容易在分析器中发现。

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

C ++界面使用

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;

其他数据类型(如 CvPoint )转换。 CvSeq 替换为 std :: vector< T>

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

这篇关于在OpenCV应用程序中,如何识别内存泄漏的来源并解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆