Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Fix itt resource memory leak issue (#33887)

liang3b56998be17e
diff --git a/cmake/developer_package/frontends/frontends.cmake b/cmake/developer_package/frontends/frontends.cmake
index e717d02ed6f..06c3ad1b3e0 100644
--- a/cmake/developer_package/frontends/frontends.cmake
+++ b/cmake/developer_package/frontends/frontends.cmake
@@ -203,7 +203,7 @@ macro(ov_add_frontend)
# Shutdown protobuf when unloading the frontend dynamic library
if(OV_FRONTEND_PROTOBUF_REQUIRED AND BUILD_SHARED_LIBS)
- target_link_libraries(${TARGET_NAME} PRIVATE openvino::protobuf_shutdown)
+ target_link_libraries(${TARGET_NAME} PRIVATE openvino::protobuf_shutdown openvino::shutdown)
endif()
if(NOT BUILD_SHARED_LIBS)
diff --git a/cmake/developer_package/plugins/plugins.cmake b/cmake/developer_package/plugins/plugins.cmake
index 119a1454508..71279b5a560 100644
--- a/cmake/developer_package/plugins/plugins.cmake
+++ b/cmake/developer_package/plugins/plugins.cmake
@@ -72,7 +72,9 @@ function(ov_add_plugin)
endif()
target_compile_definitions(${OV_PLUGIN_NAME} PRIVATE IMPLEMENT_OPENVINO_RUNTIME_PLUGIN)
- if(NOT BUILD_SHARED_LIBS)
+ if(BUILD_SHARED_LIBS)
+ target_link_libraries(${OV_PLUGIN_NAME} PRIVATE openvino::shutdown)
+ else()
# to distinguish functions creating plugin objects
target_compile_definitions(${OV_PLUGIN_NAME} PRIVATE
OV_CREATE_PLUGIN=create_plugin_engine_${OV_PLUGIN_DEVICE_NAME})
diff --git a/src/cmake/openvino.cmake b/src/cmake/openvino.cmake
index ef9be523e6e..366531627eb 100644
--- a/src/cmake/openvino.cmake
+++ b/src/cmake/openvino.cmake
@@ -58,6 +58,10 @@ target_link_libraries(${TARGET_NAME}
PUBLIC $<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.1>>:stdc++fs>
$<$<AND:$<CXX_COMPILER_ID:Clang>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:c++fs>)
+if(BUILD_SHARED_LIBS)
+ target_link_libraries(${TARGET_NAME} PRIVATE openvino::shutdown)
+endif()
+
if (TBBBIND_2_5_FOUND)
target_link_libraries(${TARGET_NAME} PRIVATE ${TBBBIND_2_5_IMPORTED_TARGETS})
endif()
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 6cf34afe6e0..2f8d0ff3848 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -2,6 +2,8 @@
# SPDX-License-Identifier: Apache-2.0
#
+add_subdirectory(shutdown)
+
add_subdirectory(itt)
add_subdirectory(conditional_compilation)
add_subdirectory(util)
diff --git a/src/common/itt/CMakeLists.txt b/src/common/itt/CMakeLists.txt
index 83bd6b72276..f65570bec3c 100644
--- a/src/common/itt/CMakeLists.txt
+++ b/src/common/itt/CMakeLists.txt
@@ -11,7 +11,7 @@ add_library(${TARGET_NAME} STATIC ${SOURCES})
add_library(openvino::itt ALIAS ${TARGET_NAME})
set_target_properties(${TARGET_NAME} PROPERTIES EXPORT_NAME itt)
-target_link_libraries(${TARGET_NAME} PUBLIC openvino::util)
+target_link_libraries(${TARGET_NAME} PUBLIC openvino::util openvino::shutdown)
if(NOT ENABLE_PROFILING_ITT STREQUAL "OFF")
if(TARGET ittapi::ittnotify)
diff --git a/src/common/itt/include/openvino/itt.hpp b/src/common/itt/include/openvino/itt.hpp
index c95f2be1f24..e91debe31d2 100644
--- a/src/common/itt/include/openvino/itt.hpp
+++ b/src/common/itt/include/openvino/itt.hpp
@@ -48,6 +48,7 @@ void taskEnd(domain_t d);
void threadName(const char* name);
void regionBegin(domain_t d, handle_t t);
void regionEnd(domain_t d);
+void shutdown();
} // namespace internal
/**
* @endcond
diff --git a/src/common/itt/src/itt.cpp b/src/common/itt/src/itt.cpp
index afc311d6b08..a5ebe811fe8 100644
--- a/src/common/itt/src/itt.cpp
+++ b/src/common/itt/src/itt.cpp
@@ -9,6 +9,8 @@
#include <mutex>
#include <vector>
+#include "openvino/shutdown.hpp"
+
#ifdef ENABLE_PROFILING_ITT
# include <ittnotify.h>
#endif
@@ -108,6 +110,10 @@ void regionEnd(domain_t d) {
current_region_handle = nullptr;
}
+void shutdown() {
+ __itt_release_resources();
+}
+
#else
domain_t domain(const char*) {
@@ -128,8 +134,16 @@ void regionBegin(domain_t, handle_t) {}
void regionEnd(domain_t) {}
+void shutdown() {}
+
#endif // ENABLE_PROFILING_ITT
} // namespace internal
} // namespace itt
} // namespace openvino
+
+static void shutdown_itt_resources() {
+ openvino::itt::internal::shutdown();
+}
+
+OV_REGISTER_SHUTDOWN_CALLBACK(shutdown_itt_resources)
diff --git a/src/common/shutdown/CMakeLists.txt b/src/common/shutdown/CMakeLists.txt
new file mode 100644
index 00000000000..6f49137d8f6
--- /dev/null
+++ b/src/common/shutdown/CMakeLists.txt
@@ -0,0 +1,18 @@
+# Copyright (C) 2018-2026 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+#
+
+set(TARGET_NAME openvino_shutdown)
+
+add_library(${TARGET_NAME} STATIC shutdown.cpp)
+
+add_library(openvino::shutdown ALIAS ${TARGET_NAME})
+target_include_directories(${TARGET_NAME} PUBLIC
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
+set_target_properties(${TARGET_NAME} PROPERTIES EXPORT_NAME shutdown)
+ov_add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
+
+# install & export
+ov_install_static_lib(${TARGET_NAME} ${OV_CPACK_COMP_CORE})
+ov_developer_package_export_targets(TARGET openvino::shutdown
+ INSTALL_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include/")
diff --git a/src/common/shutdown/include/openvino/shutdown.hpp b/src/common/shutdown/include/openvino/shutdown.hpp
new file mode 100644
index 00000000000..d4dc2ce2f10
--- /dev/null
+++ b/src/common/shutdown/include/openvino/shutdown.hpp
@@ -0,0 +1,16 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#pragma once
+#include <functional>
+#include <vector>
+
+namespace ov {
+ // Registers a shutdown callback
+ bool register_shutdown_callback(const std::function<void()>& func);
+}
+
+#define OV_REGISTER_SHUTDOWN_CALLBACK(func) \
+ namespace { \
+ static bool ov_shutdown_register_##func = ov::register_shutdown_callback(func); \
+ }
diff --git a/src/common/shutdown/shutdown.cpp b/src/common/shutdown/shutdown.cpp
new file mode 100644
index 00000000000..c6c510380b4
--- /dev/null
+++ b/src/common/shutdown/shutdown.cpp
@@ -0,0 +1,42 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "openvino/shutdown.hpp"
+
+#include <functional>
+#include <vector>
+
+namespace ov {
+
+class ShutdownRegistry {
+private:
+ std::vector<std::function<void()>> m_callbacks;
+ ShutdownRegistry() = default;
+public:
+
+ static ShutdownRegistry& get() {
+ static ShutdownRegistry instance;
+ return instance;
+ }
+
+ bool register_callback(const std::function<void()>& func) {
+ if (!func) {
+ return false;
+ }
+ m_callbacks.emplace_back(func);
+ return true;
+ }
+
+ ~ShutdownRegistry() {
+ for (auto& func : m_callbacks) {
+ func();
+ }
+ m_callbacks.clear();
+ }
+};
+
+bool register_shutdown_callback(const std::function<void()>& func) {
+ return ShutdownRegistry::get().register_callback(func);
+}
+
+} // namespace ov
diff --git a/src/frontends/common/shutdown_protobuf/CMakeLists.txt b/src/frontends/common/shutdown_protobuf/CMakeLists.txt
index d0c945ed45b..d195d7fde47 100644
--- a/src/frontends/common/shutdown_protobuf/CMakeLists.txt
+++ b/src/frontends/common/shutdown_protobuf/CMakeLists.txt
@@ -14,3 +14,4 @@ target_include_directories(${TARGET_NAME} SYSTEM PRIVATE
$<BUILD_INTERFACE:$<TARGET_PROPERTY:protobuf::libprotobuf,INTERFACE_INCLUDE_DIRECTORIES>>)
set_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE ${ENABLE_LTO})
target_compile_features(${TARGET_NAME} PRIVATE $<TARGET_PROPERTY:protobuf::libprotobuf,INTERFACE_COMPILE_FEATURES>)
+target_link_libraries(${TARGET_NAME} PRIVATE openvino::shutdown)
diff --git a/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp b/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp
index 955ea720da3..78a8935ca0a 100644
--- a/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp
+++ b/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp
@@ -4,28 +4,10 @@
#include <google/protobuf/stubs/common.h>
-#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)
-# include <windows.h>
-BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
- DWORD fdwReason, // reason for calling function
- LPVOID lpReserved) // reserved
-{
- // Perform actions based on the reason for calling.
- switch (fdwReason) {
- case DLL_PROCESS_ATTACH:
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- break;
+#include "openvino/shutdown.hpp"
- case DLL_PROCESS_DETACH:
- google::protobuf::ShutdownProtobufLibrary();
- break;
- }
- return TRUE; // Successful DLL_PROCESS_ATTACH.
-}
-#elif defined(__linux__) || defined(__APPLE__)
-extern "C" __attribute__((destructor)) void library_unload();
-void library_unload() {
+static void shutdown_frontend_resources() {
google::protobuf::ShutdownProtobufLibrary();
}
-#endif
+
+OV_REGISTER_SHUTDOWN_CALLBACK(shutdown_frontend_resources)