释放 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.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:最好的方法是激活每个 matVec[k] 对象的析构函数。您可以通过让
matVec
超出范围或将matVec
定义为Mat*
的向量
来实现此目的,并且手动分配和取消分配每个对象。这总是更安全,因为 OpenCV 对
Mat
对象使用引用计数机制,因此您不想直接释放Mat
中的数据。The best way would be to activate the destructor of each
matVec[k]
object. You can do this by lettingmatVec
fall out of scope or by definingmatVec
as avector
ofMat*
and manually allocating and deallocating each object.This is always safer since OpenCV using a reference counting mechanism for
Mat
objects and so you don't want to deallocate the data inMat
directly.