cv::Mat's release method
Asked Answered
P

4

8

I would like to confirm whether cv::Mat::release() method is similar to free() in C programming, i.e., its deallocates the Matrix data from the memory.

In particular, I would like to understand the behaviour of this method with respect to memory leaks and make sure there is no leak in may programs.

Philter answered 8/4, 2013 at 7:30 Comment(2)
Encapsulate your code inside a while(true){//loop} and let it run. In the meanwhile, monitor your memory with a task manager to reason about your memory.Lunnete
There are specific tools to measure memory leaks and other programming mistakes (using uninitialized variables etc). Take a look at «valgrind».Richrichara
N
9

If the reference counter is equal to one, then yes, cv::Mat::release() will decrement it to zero and deallocate the structure (like free in C).

If the reference counter is greater than one (ie. there's some other object interested in the structure), then cv::Mat::release() will only decrement the reference counter.

You can increment the reference counter of a cv::Mat structure (that is, to flag that you are interested in it and you don't want it to be deallocated) by invoking the cv::Mat::addref() method.

Nightwear answered 8/4, 2013 at 8:6 Comment(1)
I have a cv::Mat v, and that is v.create() inside a loop and the size of v is different for different iteration. then for the next iteration I like to remove the old one for creating the new one. Can i use release() or deallocate()?Philter
C
6

you don't have to manually deallocated cv::Mat objects since it is automatically managed , unless you have initialized the Mat from an Iplimage in this case you should manually deallocate it deallocate().

please refer to this thread .

openCV mixing IplImage with cv::Mat

Curling answered 8/4, 2013 at 9:27 Comment(0)
T
6

I was having memory leaks using a code structure like this (OpenCV with C++):

int i;
while(true){
    Mat x = imread("C:/pics"+i+".jpg");
    //do something with x
}

After 100 or so iterations it always crashed, then i changed the code to this:

int i;
while(true){
    Mat x = imread("C:/pics"+i+".jpg");
    //do something with x
    x.refcount = 0;
    x.release();
}

It stopped crashing and did the full iteration. But when setting the refcount manually to 0 you must be really sure that you dont need the object anymore.

Triplett answered 13/4, 2014 at 10:10 Comment(1)
Could you clarify the reason for your down-vote? Is it because youre missing my point?Triplett
R
0

The following program snippet tests the behavior of Mat::release() (adapted from opencv-org-answer)

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
    cout << "test_mat_release.cpp" << endl;

    {
        //memory is released
        const char *pzInputImg = "data/em_sample.png";

        Mat img;
        img = imread(pzInputImg);
        assert(img.data);
        img.release();
        assert(!img.data);
    }
    {
        //memory is released
        Mat img(100, 100, CV_32FC1);
        assert(img.data);
        img.release();
        assert(!img.data);
    }

    {   
        //Since Mat does not owns data , data is not released
        //you have to explicitly release data
        int cols=17;
        int rows=15;
        unsigned char * data = new unsigned char[rows*cols*3];
        Mat img(rows, cols, CV_8UC3, data);
        assert(img.data);
        img.release();
        assert(!img.data);
        delete [] data;
    }



    Mat imgRef;
    {
        //memory is not released
        //since there img.data is now shared with imgRef
        Mat img(100, 100, CV_32FC1);
        imgRef=img;
        assert(img.data);
        assert(img.data == imgRef.data);
        img.release();
        assert(img.empty());
        assert(!img.data);
        //img.data is NULL, but imgRef.data is not NULL, so data is not de-allocated
        assert(!imgRef.empty());
        assert(imgRef.data);
    }
    return 0;
}
Reld answered 29/10, 2016 at 23:49 Comment(2)
would be super great if in the last example you show a way to release memory, I suppose by releasing imgRef and then img...?Net
What is the reference is to a crop? like Mat imgRef = img(roi); what happens if I then release img and then try to also release imgRef?Beecher

© 2022 - 2024 — McMap. All rights reserved.