Ansu Ansu - 11 days ago 5
C++ Question

what happens when we overwrite cv::Mat.data

What will happen when we manually add an address of a buffer in

data
attributes of a
cv::Mat
object, and later delete that buffer?

For example,

cv::Mat test;
test.data = (address of Buffer A);


what will happen to
test.data
when
Buffer A
is deleted?

Answer

Documentation: http://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html

int rows, cols, type; // you need initialize them
void* data = (address of Buffer A)
cv::Mat test = cv::Mat(rows, cols, type, data); // according to documentation, test does not own data
cv::Mat copy = test.clone() // copy copies is a deep copy of test 

So since test does not own Buffer A, once it's delete, if you access test.data, it's UB. However, since copy is a deep copy, you can access copy.data

Comments