释放 cv::Mat 而不释放内存
我将 Mat 数据(不是 cv::Mat 对象)传递给函数,并使该函数成为该数据的新所有者。但是,我需要一种方法来释放原始对象,而不释放它指向的数据缓冲区。
我知道这种情况会发生在从外部数据创建的 cv::Mat 上,我只需要将此功能用于一般的 cv::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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
addref()
方法,但是您将出现内存泄漏。实际上,将数据与 Mat 分离并不是一个好主意:
因此,Mat 只能保证支持两种方式:
任何其他方式即使在当前版本中有效,也可能在未来版本中被破坏。
You can use
addref()
method but you will have a memory leak.Actually it is not a good idea to detach data from Mat:
So there are only two ways that Mat is guaranteed to support:
Any other way can be broken in future versions even if it works in current.
您可以使用 lambda 捕获 Mat,并且可以使用该 lambda 作为删除器来延长其生命周期并将其绑定到 shared_ptr/unique_ptr。
当您不想在公共接口上使用 opencv,并且无法提供有关 Mat 创建的指针或不想将数据复制到缓冲区时,它非常有用。
You can capture the Mat with a lambda, and you can use this lambda as a deleter to extend and bound its life to a shared_ptr/unique_ptr.
Its useful when you don't want to use the opencv on your public interface, and you can't provide your pointer on Mat creation or don't want to copy data to your buffer.
使用 release()
Ptr
http://opencv.willowgarage.com /documentation/cpp/basic_structs.html
智能引用计数指针的模板
类 Ptr<_Tp> 类是一个包装相应类型指针的模板类。它类似于shared_ptr,它是Boost库的一部分(http: //www.boost.org/doc/libs/1_40_0/libs/smart_ptr/shared_ptr.htm )以及一部分C++0x 标准。
通过使用该类,您可以获得以下功能:
Ptr 类将包装对象视为黑盒,引用计数器单独分配和管理。指针类需要了解该对象的唯一事情是如何释放它。这些知识被封装在 Ptr::delete_obj() 方法中,当引用计数器变为 0 时调用该方法。如果对象是 C++ 类实例,则不需要额外的编码,因为该方法的默认实现调用 delete obj; 。但是,如果以不同方式释放对象,则应创建专门的方法。例如,如果你想包装 FILE ,delete_obj 可以实现如下:
template<>内联 void Ptr::delete_obj()
{
fclose(obj); // 之后不需要清除指针,
// 这是在外部完成的。
}
...
// 现在使用它:
Ptr f(fopen("myfile.txt", "r"));
if(f.empty())
扔 ...;
fprintf(f, ....);
...
// 文件将被 Ptr 析构函数自动关闭。
注意:引用递增/递减操作是作为原子操作实现的,因此在多线程应用程序中使用这些类通常是安全的。对于在引用计数器上运行的 Mat 和其他 C++ OpenCV 类也是如此。
USE release()
Ptr
http://opencv.willowgarage.com/documentation/cpp/basic_structures.html
A template class for smart reference-counting pointers
The class Ptr<_Tp> is a template class that wraps pointers of the corresponding type. It is similar to shared_ptr that is a part of Boost library ( http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/shared_ptr.htm ) and also a part of the C++0x standard.
By using this class you can get the following capabilities:
The class Ptr treats the wrapped object as a black box, the reference counter is allocated and managed separately. The only thing the pointer class needs to know about the object is how to deallocate it. This knowledge is incapsulated in Ptr::delete_obj() method, which is called when the reference counter becomes 0. If the object is a C++ class instance, no additional coding is needed, because the default implementation of this method calls delete obj; . However, if the object is deallocated in a different way, then the specialized method should be created. For example, if you want to wrap FILE , the delete_obj may be implemented as following:
template<> inline void Ptr::delete_obj()
{
fclose(obj); // no need to clear the pointer afterwards,
// it is done externally.
}
...
// now use it:
Ptr f(fopen("myfile.txt", "r"));
if(f.empty())
throw ...;
fprintf(f, ....);
...
// the file will be closed automatically by the Ptr destructor.
Note : The reference increment/decrement operations are implemented as atomic operations, and therefore it is normally safe to use the classes in multi-threaded applications. The same is true for Mat and other C++ OpenCV classes that operate on the reference counters.