Mat在convertTo函数中的内存泄漏 [英] Memory leak with Mat in convertTo-function

查看:311
本文介绍了Mat在convertTo函数中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some trouble with managing memory in my function. Valgrid says I'm having a memory leak after the convert-function. Could it be because of the data is not being properly released? I've tried to use temp-pointers, but my program either crashes or is not working properly. Have someone encountered this problem before?

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的输出:

This is the valgrid output:

==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)

推荐答案

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

please don't store pointers to Mat in your vector(or anywhere else !).

这些东西像smartpointer一样在内部被重新引用,并且您通过存储/复制指针来破坏这一点(无论如何,指向smartpointer的指针向量听起来都是很愚蠢的,不是吗?)

those things are refcounted internally, like smartpointers, and you're wrecking that by storing/copying pointers (a vector of pointers to smartpointers would sound silly anyway, no?).

使用普通的vector<Mat>,在今晚与声音睡眠进行交易时,每一项都需要额外〜56个字节;)

use a plain vector<Mat>, trade some additional ~56 bytes per item against sound sleep tonight ;)

这篇关于Mat在convertTo函数中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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