diff --git a/modules/ocl/src/cl_operations.cpp b/modules/ocl/src/cl_operations.cpp index 7ed1a79c8..032ebe82a 100644 --- a/modules/ocl/src/cl_operations.cpp +++ b/modules/ocl/src/cl_operations.cpp @@ -192,6 +192,7 @@ void openCLMallocPitchEx(Context *ctx, void **dev_ptr, size_t *pitch, clFinish(getClCommandQueue(ctx)); #endif CheckBuffers data(mainBuffer, size, widthInBytes, height); + cv::AutoLock lock(getInitializationMutex()); __check_buffers.insert(std::pair((cl_mem)*dev_ptr, data)); } #endif @@ -253,10 +254,17 @@ void openCLFree(void *devPtr) bool failBefore = false, failAfter = false; #endif CheckBuffers data; - std::map::iterator i = __check_buffers.find((cl_mem)devPtr); - if (i != __check_buffers.end()) { - data = i->second; + cv::AutoLock lock(getInitializationMutex()); + std::map::iterator i = __check_buffers.find((cl_mem)devPtr); + if (i != __check_buffers.end()) + { + data = i->second; + __check_buffers.erase(i); + } + } + if (data.mainBuffer != NULL) + { #ifdef CHECK_MEMORY_CORRUPTION Context* ctx = Context::getContext(); std::vector checkBefore(__memory_corruption_guard_bytes); @@ -286,7 +294,6 @@ void openCLFree(void *devPtr) clFinish(getClCommandQueue(ctx)); #endif openCLSafeCall(clReleaseMemObject(data.mainBuffer)); - __check_buffers.erase(i); } #if defined(CHECK_MEMORY_CORRUPTION) if (failBefore)