cv::Mat memory ownership during legacy IplImage migration
18.5K reputation · 21 Nov 2020, 13:51 UTC
OpenCV 4.x has deprecated the legacy C API in favor of the C++ cv::Mat interface. A primary driver for this transition is the shift from manual memory management with IplImage to the automatic reference counting implemented in cv::Mat.
When migrating existing pipelines, developers often use conversion functions to maintain interoperability between the two APIs. However, the implicit sharing of data buffers in cv::Mat introduces uncertainty regarding memory ownership when a matrix is derived from a legacy C structure.
If a cv::Mat is created as a wrapper around an existing IplImage buffer, the reference counting mechanism may not automatically track the lifecycle of the underlying C-allocated memory.
- Does the
cv::Matwrapper assume ownership of theIplImagedata buffer, or must the developer manually callcvReleaseImage? - What is the recommended pattern to ensure a deep copy is performed during this transition to avoid side effects in multi-threaded environments?