在convertTo函数中使用Mat进行内存泄漏

时间:2014-05-13 12:56:59

标签: opencv memory-management valgrind mat

在我的功能中管理内存时遇到了一些麻烦。 Valgrid说我在转换函数后有内存泄漏。可能是因为数据未正确发布?我试过使用临时指针,但我的程序崩溃或无法正常工作。以前有人遇到过这个问题吗?

this->images.push_back(new cv::Mat());                                                          //ID
cv::threshold(*this->images[MASK], *this->images[ID], 0.0, 1.0, cv::THRESH_BINARY);
this->images[ID]->convertTo(*this->images[ID], CV_32SC1);

这是valgrid输出:

==5663== 64,339,996 bytes in 1 blocks are possibly lost in loss record 380 of 380
==5663==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5663==    by 0x4E95117: cv::fastMalloc(unsigned long) (in /usr/local/lib/libopencv_core.so.2.4.9)
==5663==    by 0x4F31F38: cv::Mat::create(int, int const*, int) (in /usr/local/lib/libopencv_core.so.2.4.9)
==5663==    by 0x4F39CF9: cv::_OutputArray::create(cv::Size_<int>, int, int, bool, int) const (in /usr/local/lib/libopencv_core.so.2.4.9)
==5663==    by 0x4EB9373: cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const (in /usr/local/lib/libopencv_core.so.2.4.9)
==5663==    by 0x40D168: DataFrame::init() (DataFrame.cpp:68)
==5663==    by 0x40C943: DataFrame::DataFrame(char const*, LeafClassifier*) (DataFrame.cpp:31)
==5663==    by 0x414A19: DataHandler::loadFrame() (DataHandler.cpp:68)
==5663==    by 0x406680: main (main.cpp:58)

1 个答案:

答案 0 :(得分:6)

不要在您的向量中存储指向Mat的指针(或其他任何地方!)。

这些东西在内部被重新计算,比如智能指针,你通过存储/复制指针来破坏它(指向智能指针的指针听起来很傻,不是吗?)。

使用普通vector<Mat>,每个项目额外交换约56个字节,以防止今晚的声音睡眠;)