【发布时间】:2015-07-27 18:06:52
【问题描述】:
我之前看到过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
这会在中途中断,否则最终会冻结我的笔记本电脑。
所以我的问题是:我做错了什么吗?
谢谢!
【问题讨论】:
-
你有一个
Queue的void*??? -
@NathanOliver 是的,我觉得这不是一个好习惯,但我是一名 Java 程序员,我无法掌握 C++ 模板作为类比 Java 泛型。另外,队列将运行时定义的块联系在一起,所以我不知道它们的类型。无论如何,我认为这不是我当前问题的原因。
-
@rhobincu 也许
ImageFeatures的一个或多个实例没有被释放。您是否在代码中的某处使用new ImageFeatures();或类似名称? -
@PaulMcKenzie 是的,我是,但我认为我正确地删除了它们,因为 Valgrind 没有报告该内存被泄露。我会再次检查,确定,我会告诉你的。
标签: c++ opencv memory-leaks