Search code examples
c++opencvmemory-managementmat

Will a Mat pointer be automatically released when being reassigned or as an ROI, source Mat is released?


I'm trying to understand clearly how Mat (OpenCV) works in many specific cases. The cases I'm inquiring in this post are the following:

  1. When a Mat pointer is reassigned to other location, will the previous header and data memory be freed automatically? for example:

    Mat *m = XXX;
    m = YYY;
    
  2. If I call sourceMat.release(), does it also release the header m?

    Mat m = sourceMat(Rect);
    

Solution

  • For the 2nd question, the answer is NO.

    From the OpenCV's doc, Mat Mat::operator()(const Rect& roi) const will make a new header. So releasing the sourceMat will not affect m.