且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

free cv :: Mat不释放内存

更新时间:2023-01-11 18:36:48

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.