Skip to content

Commit 77efa70

Browse files
committed
Destroy zeroApi safe
Signed-off-by: Bogdan Pereanu <[email protected]>
1 parent 1af45a5 commit 77efa70

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_api.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ZeroApi {
7878

7979
~ZeroApi() = default;
8080

81-
static const std::shared_ptr<ZeroApi>& getInstance();
81+
static const std::shared_ptr<ZeroApi> getInstance();
8282

8383
#define symbol_statement(symbol) decltype(&::symbol) symbol;
8484
symbols_list();

src/plugins/intel_npu/src/utils/src/zero/zero_api.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "intel_npu/utils/zero/zero_api.hpp"
66

7+
#include <mutex>
8+
79
#include "openvino/util/file_util.hpp"
810
#include "openvino/util/shared_object.hpp"
911

@@ -49,8 +51,16 @@ ZeroApi::ZeroApi() {
4951
#undef symbol_statement
5052
}
5153

52-
const std::shared_ptr<ZeroApi>& ZeroApi::getInstance() {
53-
static std::shared_ptr<ZeroApi> instance = std::make_shared<ZeroApi>();
54+
const std::shared_ptr<ZeroApi> ZeroApi::getInstance() {
55+
static std::mutex mutex;
56+
static std::weak_ptr<ZeroApi> weak_instance;
57+
58+
std::lock_guard<std::mutex> lock(mutex);
59+
auto instance = weak_instance.lock();
60+
if (!instance) {
61+
instance = std::make_shared<ZeroApi>();
62+
weak_instance = instance;
63+
}
5464
return instance;
5565
}
5666

0 commit comments

Comments
 (0)