5

当我尝试创建一个 cv::Mat 并在之后释放它(下面的代码)时,我仍然有(根据 valgrind)每个像素大约 1 个字节的内存泄漏。

有谁知道如何正确释放 cv::Mat 的内存?

感谢您的回答:)

代码:

int main(int argc, char** argv)
{
  cv::Mat* matrx = new cv::Mat(1000,1000,CV_8UC1,0.);
  matrx->release();
  delete matrx;
  return 0;
}

瓦尔格林:

[...]
==29420==    1,000,028 bytes in 1 blocks are definitely lost in loss record 372 of 372
==29420==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==29420==    by 0x5438877: cv::fastMalloc(unsigned long) (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8)
==29420==    by 0x536FE2A: cv::Mat::create(int, int const*, int) (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8)
==29420==    by 0x426FB5: cv::Mat::create(int, int, int) (mat.inl.hpp:663)
==29420==    by 0x426ECD: cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&) (mat.inl.hpp:347)
==29420==    by 0x425A09: main (main.cpp:18)
==29420== 
==29420== LEAK SUMMARY:
==29420==    definitely lost: 1,000,028 bytes in 1 blocks
==29420==    indirectly lost: 0 bytes in 0 blocks
==29420==      possibly lost: 5,072 bytes in 95 blocks
==29420==    still reachable: 304,758 bytes in 1,348 blocks
==29420==         suppressed: 0 bytes in 0 blocks
[...]
4

2 回答 2

4

来自 opencv github 的反馈似乎很清楚:您使用 OpenCV 3.x 编译,但在运行时使用 OpenCV 2.4.8。由于它们不是二进制兼容的,因此它不能正确释放cv::Mat. 让您导出LD_LIBRARY_PATHOCV_DIST/lib用于编译的 OpenCV 3.x。

请注意,如果您之前delete甚至不需要指针release()

于 2016-09-23T09:45:48.947 回答
1

从文档页面引用以下内容:(http://docs.opencv.org/2.4.9/modules/core/doc/basic_structures.html#mat-release):

void Mat::release() 该方法递减与矩阵数据关联的引用计数器。当引用计数器达到 0 时,矩阵数据被释放,数据和引用计数器指针被设置为 NULL。

上面的意思是当你做“删除矩阵;” 您正在尝试删除已经指向 NULL 的内存区域,这会产生运行时错误。

于 2016-11-18T02:02:19.567 回答