I have a rather really simple piece of code, I generate some cv::Mat images inside a function (myFunction) and fill an std::vector with them, whilst the vector is been filled the memory usage increase and after the vector goes out of scope the memory it used is freed as expected.
But for some reason if I execute cv::cvtColor before adding the Mat to the vector, the memory is not freed after the vector goes out of scope. Also, if I iterate over the vector and execute the cv::imwrite function before the vector goes out of scope the memory is not freed either.
These two problems only occur if I execute myFunction in an std::thead, if I don't use std:thread there is no memory leak at all.
This is my code:
void myFunction(){;
int x = 0;
std::vector<cv::Mat> myvector;
while(x < 1000){
cv::Mat oImage(1000, 2000, CV_8UC3, cv::Scalar(255,0,255));
//cv::cvtColor(oImage,oImage,cv::COLOR_BGR2RGB); IF I UNCOMMENT THIS LINE THE LEAK OCCURS WHEN USING STD:THREAD
myvector.push_back(oImage);
x++;
}
/* ALSO IF I DON'T EXECUTE cvtColor BUT I UNCOMMENT THESE 4 LINES THE LEAK ALSO OCCURS WHEN USING STD::THREAD
int _size = myvector.size();
for(int i = 0; i < _size; i++){
imwrite("/home/admin/test/"+std::to_string(i)+".jpg", myvector[i]);
}
*/
myvector.clear();
};
int main(int argc, char** argv)
{
int y =0;
while(y<20){
std::thread t(myFunction);
t.join();
//myFunction(); //NO PROBLEM OCCURS IF I JUST CALL THIS FUNCTION WITHOUT USING STD::THREAD
std::cout << "out of scope"; //HERE THE MEMORY SHOULD HAVE BEEN FREED
getchar();
}
return 0;
}
I'm using OpenCV 4.7, I think there was a problem with opencv and thread when using version 3.2.0, but it should be fix right now.