Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv::Mat Pointers, Scope and Threading

Hello,

I would like to know about smart pointers and thread safety. If we take a simple example we create an image and pass it to two (hypothetical) 'threaded' functions. They use the cv::Mat, not a cv::Mat* or cv::Mat&, the idea being that when they go out of scope, they will be automatically released.

void functionX(cv::Mat image)
{
    //Do some threading things
    //Async long running.
}

void functionY(cv::Mat image)
{
    //Do some other threading things
    //Async long running.
}

main()
{
    cv::Mat img = cv::imread('someImage.png',1);

    functionX(img);
    functionY(img);

}

Will the cv::Mat deallocate itself? I have read about smart pointers and they can make the object they are pointing to thread safe. With the std::shared_ptr() it is thread safe so long as two threads try not to write to the same shared_ptr() object, instead of two separate shared_ptrs to the base object.

Is this the case for OpenCV's cv::Mat?

When we call functionX or functionY, with the above call signatures, is a new cv::Mat header created for each of those functions? Headers that reference the same memory for the object img?

Will they be garbage collected when it goes out of scope?

Regards,

Daniel