From 55409e43a3f69a4e67e7f30558d12ba8c198a438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Mestre?= Date: Fri, 27 Oct 2023 17:46:52 +0100 Subject: [PATCH] [OpenCL] Fix memory leak --- source/adapters/opencl/adapter.cpp | 16 ++++++++++------ source/adapters/opencl/common.hpp | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/source/adapters/opencl/adapter.cpp b/source/adapters/opencl/adapter.cpp index 10713b9ff9..65c5676bf9 100644 --- a/source/adapters/opencl/adapter.cpp +++ b/source/adapters/opencl/adapter.cpp @@ -12,21 +12,17 @@ struct ur_adapter_handle_t_ { std::atomic RefCount = 0; + std::mutex Mutex; }; ur_adapter_handle_t_ adapter{}; UR_APIEXPORT ur_result_t UR_APICALL urInit(ur_device_init_flags_t, ur_loader_config_handle_t) { - cl_ext::ExtFuncPtrCache = new cl_ext::ExtFuncPtrCacheT(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urTearDown(void *) { - if (cl_ext::ExtFuncPtrCache) { - delete cl_ext::ExtFuncPtrCache; - cl_ext::ExtFuncPtrCache = nullptr; - } return UR_RESULT_SUCCESS; } @@ -34,6 +30,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, uint32_t *pNumAdapters) { if (NumEntries > 0 && phAdapters) { + std::lock_guard Lock{adapter.Mutex}; + if (adapter.RefCount++ == 0) { + cl_ext::ExtFuncPtrCache = std::make_unique(); + } + *phAdapters = &adapter; } @@ -50,7 +51,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) { } UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) { - --adapter.RefCount; + std::lock_guard Lock{adapter.Mutex}; + if (--adapter.RefCount == 0) { + cl_ext::ExtFuncPtrCache.reset(); + } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/common.hpp b/source/adapters/opencl/common.hpp index f78710d0df..95105b552d 100644 --- a/source/adapters/opencl/common.hpp +++ b/source/adapters/opencl/common.hpp @@ -260,7 +260,7 @@ struct ExtFuncPtrCacheT { // piTeardown to avoid issues with static destruction order (a user application // might have static objects that indirectly access this cache in their // destructor). -inline ExtFuncPtrCacheT *ExtFuncPtrCache; +inline std::unique_ptr ExtFuncPtrCache; // USM helper function to get an extension function pointer template