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:
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.
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.
Mat matrix;
...
std::shared_ptr<uchar*> ptr(matrix.ptr(),[matrix](uchar*){});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With