<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 3.4.3</li>
<li>Operating System / Platform =&gt; Android ARMv7</li>
<li>Compiler =&gt; Using official binaries</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">I previously used OpenCV 3.1 that I manually compiled to use libjpeg-turbo. When I switched to OpenCV 3.4.3, I saw that libjpeg-turbo was now used instead of libjpeg so I used the official Android binaries for both OpenCV and 3rd party libraries.</p>
<p dir="auto">However, reading a JPEG image from disk went from 360ms to 800ms (with <code class="notranslate">IMREAD_COLOR</code>) and from 160ms to 300ms (with <code class="notranslate">IMREAD_GRAYSCALE</code>). These images don't have any exif orientation.</p>
<h5 dir="auto">Steps to reproduce</h5>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="cv::imread(path, IMREAD_COLOR)"><pre class="notranslate"><span class="pl-en">cv::imread</span>(path, IMREAD_COLOR)</pre></div>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 3.4.2</li>
<li>Operating System / Platform =&gt; Windows 10</li>
<li>Compiler =&gt; Visual Studio 2017</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">Hello there! I'm experiencing some <strong>memory leak</strong> on OpenCV when working with C++ threads. I've seen some similar problems around but not quite the same. Also, the ones I've found were not solved.</p>
<p dir="auto">I've reduced my code down to the simplest test one can make in which the problem persists.</p>
<p dir="auto">What it does: 1000 threads are spawned and each one of them will:</p>
<ul dir="auto">
<li>Create a 300x300 Mat image filled with zeros;</li>
<li>Add the image with itself and store in a new Mat. This is done 100 times within the each thread.</li>
</ul>
<p dir="auto">Memory leaks as shown below:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/13644591/43962014-e1a76a80-9c8c-11e8-8886-f6ac6009c7d2.png"><img src="https://user-images.githubusercontent.com/13644591/43962014-e1a76a80-9c8c-11e8-8886-f6ac6009c7d2.png" width="50%" style="max-width: 100%;"></a></p>
<h5 dir="auto">Steps to reproduce</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="void static thread_call(int id, int n_threads) {
    cv::Mat teste = cv::Mat(300, 300, CV_32FC1, cv::Scalar(0));	
    for (int i = 0; i &lt; 100; i++) {
        cv::Mat result;
	cv::add(teste, teste, result);
    }
}

void static test() {
    int n_threads = 1000;
    for (int i = 0; i &lt; 1000; i++) {
	//Fork
	std::thread *t = new std::thread[n_threads];
	for (int i = 0; i &lt; n_threads; ++i)
		t[i] = std::thread(thread_call, i, n_threads);
	//Join
	for (int i = 0; i &lt; n_threads; ++i)
		t[i].join();
	delete[] t;
    }
    return;
}

int main()
{
    test();
    return 0;
}
"><pre class="notranslate"><code class="notranslate">void static thread_call(int id, int n_threads) {
    cv::Mat teste = cv::Mat(300, 300, CV_32FC1, cv::Scalar(0));	
    for (int i = 0; i &lt; 100; i++) {
        cv::Mat result;
	cv::add(teste, teste, result);
    }
}

void static test() {
    int n_threads = 1000;
    for (int i = 0; i &lt; 1000; i++) {
	//Fork
	std::thread *t = new std::thread[n_threads];
	for (int i = 0; i &lt; n_threads; ++i)
		t[i] = std::thread(thread_call, i, n_threads);
	//Join
	for (int i = 0; i &lt; n_threads; ++i)
		t[i].join();
	delete[] t;
    }
    return;
}

int main()
{
    test();
    return 0;
}

</code></pre></div>
<p dir="auto">Any insights are welcome :)</p>