diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index ffea804ed98eaa6f089c27e0770c78525a5e2a52..24190c52c91649562ca1e6dd49db6dbbb1136275 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -1581,7 +1581,7 @@ void finish() #define IMPLEMENT_REFCOUNTABLE() \ void addref() { CV_XADD(&refcount, 1); } \ - void release() { if( CV_XADD(&refcount, -1) == 1 ) delete this; } \ + void release() { if( CV_XADD(&refcount, -1) == 1 && !cv::__termination) delete this; } \ int refcount /////////////////////////////////////////// Platform ///////////////////////////////////////////// diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 4b3efce4a812e621ad8b1d6ad5e9d46f493155ad..cef4db3c2c782e7f4b6849be1cfbc11b5d3f9edd 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -918,16 +918,22 @@ public: #pragma warning(disable:4447) // Disable warning 'main' signature found without threading model #endif -BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID); - +extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved) { if (fdwReason == DLL_THREAD_DETACH || fdwReason == DLL_PROCESS_DETACH) { if (lpReserved != NULL) // called after ExitProcess() call + { cv::__termination = true; - cv::deleteThreadAllocData(); - cv::deleteThreadData(); + } + else + { + // Not allowed to free resources if lpReserved is non-null + // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583.aspx + cv::deleteThreadAllocData(); + cv::deleteThreadData(); + } } return TRUE; }