释放 std::vector 内的 Mat
使用OpenCV 2.2,我试图通过调用释放来释放内存到Mat图像 在 std::vector 内,例如:
std::vector < Mat > matVec;
但是,似乎
for (int k = 0; k < matVec.size(); k++)
{
matVec[k].release();
}
没有释放任何内存(尽管它仍然可以编译)。
我知道 OpenCV 2.2 中有新的内存管理,但我找不到 问题。
使用 IplImage*
类型而不是 Mat 的类似示例(使用 cvReleaseImage()
而不是 Mat 成员函数 .release()
)效果很好。
有什么提示吗?
Using OpenCV 2.2, I am trying to free memory be calling release to a Mat image
inside a std::vector, such as:
std::vector < Mat > matVec;
However, it seems like
for (int k = 0; k < matVec.size(); k++)
{
matVec[k].release();
}
is not releasing any memory (it still compiles, though).
I know there is new memory management in OpenCV 2.2, but I couldn't spot the
issue.
A similar example with IplImage*
type instead of Mat (using cvReleaseImage()
instead of the Mat member function .release()
) works just fine.
Any hints?
.release()
仅应在特殊情况下调用,不适合日常使用。只需清除向量:这会调用每个
Mat
的析构函数,释放内存(如果没有其他cv::Mat
指向它 - 记住cv ::Mat
与IplImage
不同,它是一种引用计数数据类型(如果在其他地方引用它,则也必须清除该引用)。如果您想保留向量,但删除内容,请将代码中的.release()
行替换为:.release()
should only be called in exceptional circumstances, it's not for everyday use. Just clear the vector:This calls the destructor of each of the
Mat
s, releasing the memory (if no othercv::Mat
points to it - remembercv::Mat
is, unlikeIplImage
, a reference-counted datatype. If it is referenced somewhere else, you have to clear that reference too). If you want to keep the vector, but get rid of the contents, replace the.release()
line in your code with: