Issue 1:
System information (version)

OpenCV => 4.1.2

Operating System / Platform => Arch Linux

Compiler => g++ 9.2.0

Detailed description

I found that my application sometimes crashes on readNetFromCaffe with a segmentation fault. I believe it is caused by the opencv function not being thread safe while I call it multiple times concurrently.

Steps to reproduce

Here is a simple test application that crashes around 10% of the runs. In my real application I have less threads (usually 2...16), but 1000 threads makes the test have this bug more frequently.

#include <thread>
#include <vector>

#include <opencv2/dnn.hpp>

void test() {
cv::dnn::readNetFromCaffe(..., ...);
}

int main() {
std::vector<std::thread> threads;

for (size_t i = 0; i < 1000; ++i) {
threads.emplace_back(test);
}

for (auto& thread : threads) {
thread.join();
}
}

Helgrind finds some issues from the application:

Possible data race during write of size 8 at 0x51EB5E8 by thread #2
Locks held: none
at 0x4FA8F9E: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x492C0B2: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x5036473: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49CFF8B: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49D0477: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49C05AD: cv::dnn::dnn4_v20190902::readNetFromCaffe(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x1092DD: test() (in /home/runner/segfaulttest/main)
by 0x10AE5B: void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) (in /home/runner/segfaulttest/main)
by 0x10ADFF: std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) (in /home/runner/segfaulttest/main)
by 0x10AD9D: void std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) (in /home/runner/segfaulttest/main)
by 0x10AD5E: std::thread::_Invoker<std::tuple<void (*)()> >::operator()() (in /home/runner/segfaulttest/main)
by 0x10AD33: std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() (in /home/runner/segfaulttest/main)

This conflicts with a previous read of size 8 by thread #3
Locks held: none
at 0x492C047: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x5036473: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49CFF8B: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49D0477: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49C05AD: cv::dnn::dnn4_v20190902::readNetFromCaffe(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x1092DD: test() (in /home/runner/segfaulttest/main)
by 0x10AE5B: void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) (in /home/runner/segfaulttest/main)
by 0x10ADFF: std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) (in /home/runner/segfaulttest/main)
Address 0x51eb5e8 is in the BSS segment of /usr/lib/libopencv_dnn.so.4.1.2

----------------------------------------------------------------

Possible data race during read of size 8 at 0x51EB5E8 by thread #3
Locks held: none
at 0x4FA8FB6: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x492C0B2: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x5036473: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49CFF8B: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49D0477: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49C05AD: cv::dnn::dnn4_v20190902::readNetFromCaffe(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x1092DD: test() (in /home/runner/segfaulttest/main)
by 0x10AE5B: void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) (in /home/runner/segfaulttest/main)
by 0x10ADFF: std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) (in /home/runner/segfaulttest/main)
by 0x10AD9D: void std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) (in /home/runner/segfaulttest/main)
by 0x10AD5E: std::thread::_Invoker<std::tuple<void (*)()> >::operator()() (in /home/runner/segfaulttest/main)
by 0x10AD33: std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() (in /home/runner/segfaulttest/main)

This conflicts with a previous write of size 8 by thread #2
Locks held: none
at 0x4FA8F9E: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x492C0B2: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x5036473: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49CFF8B: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49D0477: ??? (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x49C05AD: cv::dnn::dnn4_v20190902::readNetFromCaffe(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libopencv_dnn.so.4.1.2)
by 0x1092DD: test() (in /home/runner/segfaulttest/main)
by 0x10AE5B: void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) (in /home/runner/segfaulttest/main)
Address 0x51eb5e8 is in the BSS segment of /usr/lib/libopencv_dnn.so.4.1.2

...

Here is also a gdb backtrace of the segmentation fault. Unfortunately I didn't have opencv symbols enabled, when I managed to reproduce the crash with gdb. It seems that running the application in gdb makes the bug very rare:

#0 0x00007fd9ce450470 in free () at /usr/lib/libc.so.6
#1 0x00007fd9d28584bc in () at /usr/lib/libopencv_dnn.so.4.1
#2 0x00007fd9d285a528 in () at /usr/lib/libopencv_dnn.so.4.1
#3 0x00007fd9d285c604 in cv::dnn::dnn4_v20190902::readNetFromCaffe(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () at /usr/lib/libopencv_dnn.so.4.1

Issue 2:
System information (version)

OpenCV => 4.1.2

Operating System / Platform => Gentoo Linux

Compiler => gcc (Gentoo 8.3.0-r1 p1.1) 8.3.0

Detailed description

With OpenCV-4.1.2 I've started to get random segfaults in multi-threaded program. I've managed to reproduce the problem with the very basic application, 2 threads where each of them just creates a Mat.

Steps to reproduce

#include <opencv2/core.hpp>
#include <thread>

using namespace std;
using namespace cv;

void thread1() {
Mat(10, 10, CV_16U, Scalar(0));
}

void thread2() {
Mat(10, 10, CV_16U, Scalar(0));
}

int main() {
thread th1(thread1);
thread th2(thread2);
th1.join();
th2.join();
return 0;
}

When compiled with:

g++ -g $(pkg-config --libs --cflags opencv4) -lpthread test-crash-412.cpp -o test

Will randomly crash or exit normally when run. To get a more stable repro, please run several times:

while true; do ./test; done

Running the same binary (without recompilation) against OpenCV-4.1.1 works fine without any crashes.

This is the stack trace I see:

Stack trace of thread 19341:
#0 0x00007f07d1dd9b50 cfree (libc.so.6)
#1 0x00007f07d256a5b9 n/a (/home/pro/projects/opencv-lib/opencv-4.1/install/lib64/libopencv_core.so.4.1.2)
#2 0x000055e3320da48c n/a (/tmp/test)
#3 0x000055e3320da2b8 n/a (/tmp/test)
#4 0x000055e3320da9ff n/a (/tmp/test)
#5 0x000055e3320da7f9 n/a (/tmp/test)
#6 0x000055e3320daf02 n/a (/tmp/test)
#7 0x000055e3320daec3 n/a (/tmp/test)
#8 0x000055e3320dae98 n/a (/tmp/test)
#9 0x00007f07d2159cae execute_native_thread_routine (libstdc++.so.6)
#10 0x00007f07d2288458 start_thread (libpthread.so.0)
#11 0x00007f07d1e546ef __clone (libc.so.6)