Replaced incorrect double checked locking with a static
authorSean McBride <sean@rogue-research.com>
Mon, 14 Feb 2022 21:33:04 +0000 (16:33 -0500)
committerSean McBride <sean@rogue-research.com>
Tue, 15 Feb 2022 19:24:21 +0000 (14:24 -0500)
Thread Sanitizer identified an incorrect implementation of double checked locking.

Replaced it with a static, which therefore can only be created once.

modules/core/src/matrix.cpp

index 02436aa..6a381c1 100644 (file)
@@ -176,27 +176,23 @@ public:
     }
 };
 
-namespace
+static
+MatAllocator*& getDefaultAllocatorMatRef()
 {
-    MatAllocator* volatile g_matAllocator = NULL;
+    static MatAllocator* g_matAllocator = Mat::getStdAllocator();
+    return g_matAllocator;
 }
 
 MatAllocator* Mat::getDefaultAllocator()
 {
-    if (g_matAllocator == NULL)
-    {
-        cv::AutoLock lock(cv::getInitializationMutex());
-        if (g_matAllocator == NULL)
-        {
-            g_matAllocator = getStdAllocator();
-        }
-    }
-    return g_matAllocator;
+    return getDefaultAllocatorMatRef();
 }
+
 void Mat::setDefaultAllocator(MatAllocator* allocator)
 {
-    g_matAllocator = allocator;
+    getDefaultAllocatorMatRef() = allocator;
 }
+
 MatAllocator* Mat::getStdAllocator()
 {
     CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator())