free cv :: Mat不释放内存 [英] free cv::Mat without releasing memory

查看:1598
本文介绍了free cv :: Mat不释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Mat数据(不是cv :: Mat对象)传递给函数,并使此函数成为此数据的新所有者。然而,我需要一个方法释放原始对象,而不释放它指向的数据缓冲区。



我知道这将发生在cv :: Mat创建从外部数据,我只需要使用这个功能一般cv :: Mat。



有办法吗?


<你可以使用 addref()方法,但你会有一个内存泄漏



实际上,从Mat分离数据不是一个好主意:





  • 您不能保证从常规cv :: Mat获得的指针指向已分配内存块的开头;

  • 很可能你不能自己释放那个内存,因为cv :: Mat可以使用自己的内存分配例程(有很多原因来做,例如对齐)。

  • 即使你找到一种方法来解决数据指针的所有问题,你仍然无法避免Mat引用计数器的内存泄漏。



因此,Mat只有两种保证支持的方式:




  • 提供Mat创建指针;

  • 将数据复制到缓冲区。



任何其他方式在未来的版本


I pass the Mat data (not a cv::Mat object) to a function, And make this function the new owner of this data. however, I need a method to free the original object, without freeing the data buffer it points to.

I know that this will happen to cv::Mat that were created from external data, I just need to use this feature for general cv::Mat.

Is there a way to do this?

解决方案

You can use addref() method but you will have a memory leak.

Actually it is not a good idea to detach data from Mat:

  • It was not designed for that;
  • You can not guarantee that pointer obtained from general cv::Mat points to the beginning of allocated memory block;
  • Most probably you will be unable to release that memory by yourself because cv::Mat may use own memory allocation routines (there are a lot of reasons to do that, for example alignment).
  • Even if you find a way to solve all the problems with data pointer, you still can not avoid memory leak for Mat reference counter.

So there are only two ways that Mat is guaranteed to support:

  • Provide your pointer on Mat creation;
  • Copy data to you buffer.

Any other way can be broken in future versions even if it works in current.

这篇关于free cv :: Mat不释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆