Skip to content

Commit cf4e9e5

Browse files
grundmanalalek
authored andcommitted
Update matrix.cpp
Fix race condition in getDefaultAllocator and setDefaultAllocator interaction / not threadsafe currently
1 parent d72ddc8 commit cf4e9e5

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

modules/core/src/matrix.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,19 @@ class StdMatAllocator : public MatAllocator
222222
};
223223
namespace
224224
{
225-
MatAllocator* g_matAllocator = NULL;
225+
MatAllocator* volatile g_matAllocator = NULL;
226226
}
227227

228228

229229
MatAllocator* Mat::getDefaultAllocator()
230230
{
231231
if (g_matAllocator == NULL)
232232
{
233-
g_matAllocator = getStdAllocator();
233+
cv::AutoLock lock(cv::getInitializationMutex());
234+
if (g_matAllocator == NULL)
235+
{
236+
g_matAllocator = getStdAllocator();
237+
}
234238
}
235239
return g_matAllocator;
236240
}

0 commit comments

Comments
 (0)