内存泄漏转换cv :: Mat

时间:2015-07-27 18:06:52

标签: c++ opencv memory-leaks

之前我曾问过this问题,但我遇到了类似的内存泄漏问题,没有使用任何动态内存分配。这是对cv :: Mat :: convertTo的调用,它用于将浮点Mat转换为CV_8U mat(mType为0):

void DescriptorDataConverterPipelineLevel::process() {
    Queue<void *> *inputQueue = mInputQueues.at(0);
    ImageFeatures *imageFeatures = (ImageFeatures *) inputQueue->pop();

    convertMatrix(imageFeatures->getDescriptors());

    mOutputQueue->push(imageFeatures);
}

void DescriptorDataConverterPipelineLevel::convertMatrix(cv::Mat &matrix) {
    matrix.convertTo(matrix, mType);
}

ImageFeatures :: getDescriptors方法实现如下:

class ImageFeatures {
public:

    cv::Mat &getDescriptors(){
        return mDescriptors;
    }

private:

    cv::Mat mDescriptors;
};

但是在运行valgrind时,我收到以下报告:

==9616== 1,240,114,808 bytes in 24,210 blocks are possibly lost in loss record 581 of 581
==9616==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9616==    by 0x4F5ED87: cv::fastMalloc(unsigned long) (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8)
==9616==    by 0x4EB94AA: cv::Mat::create(int, int const*, int) (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8)
==9616==    by 0x4EC0A69: cv::_OutputArray::create(cv::Size_<int>, int, int, bool, int) const (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8)
==9616==    by 0x4FD7BE3: cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8)
==9616==    by 0x418640: DescriptorDataConverterPipelineLevel::process() (DescriptorDataConverterPipelineLevel.cpp:33)
==9616==    by 0x406B28: PipelineLevel::run() (PipelineLevel.hpp:75)
==9616==    by 0x419729: Thread::execute() (Thread.cpp:39)
==9616==    by 0x5ECCA3F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==9616==    by 0x6B2A181: start_thread (pthread_create.c:312)
==9616==    by 0x663E47C: clone (clone.S:111)
==9616== 
==9616== LEAK SUMMARY:
==9616==    definitely lost: 0 bytes in 0 blocks
==9616==    indirectly lost: 0 bytes in 0 blocks
==9616==      possibly lost: 1,240,347,280 bytes in 24,478 blocks
==9616==    still reachable: 4,198,805 bytes in 49,931 blocks
==9616==         suppressed: 0 bytes in 0 blocks
==9616== Reachable blocks (those to which a pointer was found) are not shown.
==9616== To see them, rerun with: --leak-check=full --show-leak-kinds=all

这在中途中断或最终冻结我的笔记本电脑。

所以我的问题是:我做错了吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我发现了问题,并且它与应用程序的其他部分严格相关,这些部分在此处不可见。 DescriptorDataConverterPipelineLevel是一个从RepeaterPipelineLevel读取ImageFeatures对象的线程,它正在克隆存储的对象并在每次需要时将它提供给Converter:

FileReader -> Repeater -> Converter \
                                     Matcher -> Writer
Folder Reader ---------->  Converter/

但是,由于转换器和匹配器之间的队列是使用无限容量的链接列表构建的,没有任何反馈,转换器会继续从转发器读取并将数据放入队列中,即使匹配器已完成处理。

解决方案是在转发器和转换器周围交换:

FileReader -> Converter -> Repeater \
                                     Matcher -> Writer
Folder Reader ---------->  Converter/ 

现在一切正常。