Skip to content

Commit 0d68ee5

Browse files
authored
CVS-176574 : Fix memory leaks for protobuf & DataOps (#852)
* fix: fix mem leaks * fix linux builds
1 parent d951954 commit 0d68ee5

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

cmake/onnxruntime_providers_openvino.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
source_group(TREE ${ONNXRUNTIME_ROOT}/core FILES ${onnxruntime_providers_openvino_cc_srcs})
3434
onnxruntime_add_shared_library_module(onnxruntime_providers_openvino ${onnxruntime_providers_openvino_cc_srcs} "${ONNXRUNTIME_ROOT}/core/dll/onnxruntime.rc")
3535

36+
# Propagate leak check define if enabled at top level
37+
if(onnxruntime_ENABLE_MEMLEAK_CHECKER)
38+
target_compile_definitions(onnxruntime_providers_openvino PRIVATE ONNXRUNTIME_ENABLE_MEMLEAK_CHECK)
39+
endif()
40+
3641
onnxruntime_add_include_to_target(onnxruntime_providers_openvino onnxruntime_common onnx nlohmann_json::nlohmann_json)
3742
install(FILES ${PROJECT_SOURCE_DIR}/../include/onnxruntime/core/providers/openvino/openvino_provider_factory.h
3843
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/onnxruntime/)

onnxruntime/core/dll/dllmain.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/,
3030
if (lpvReserved != nullptr) {
3131
g_is_shutting_down = true;
3232
// do not do cleanup if process termination scenario
33+
#if defined(ONNXRUNTIME_ENABLE_MEMLEAK_CHECK)
34+
// In leak-check builds we still want protobuf shutdown to avoid flagged leaks.
35+
::google::protobuf::ShutdownProtobufLibrary();
36+
#endif
3337
} else {
3438
// Cleanup protobuf library.
3539
// NOTE: it might be too early to do so, as all function local statics and global objects are not destroyed yet.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Intel Corporation.
2+
// Licensed under the MIT License.
3+
#ifdef _WIN32
4+
5+
#include <Windows.h>
6+
#ifdef __GNUC__
7+
#pragma GCC diagnostic push
8+
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
9+
#pragma GCC diagnostic ignored "-Wunused-parameter"
10+
#else
11+
#endif
12+
#include <google/protobuf/message_lite.h>
13+
#ifdef __GNUC__
14+
#pragma GCC diagnostic pop
15+
#endif
16+
#include <atomic>
17+
18+
// Reuse the global shutdown indicator (do NOT set it here; that is owned by the core DLL).
19+
extern std::atomic<bool> g_is_shutting_down;
20+
21+
// NOTE:
22+
// This DllMain exists because the OpenVINO provider DLL statically links protobuf independently
23+
// of the core onnxruntime DLL. The core DLL's DllMain won't clean up this copy.
24+
// We perform protobuf shutdown on dynamic unload, and (optionally) during process termination
25+
// when memory leak checking is enabled.
26+
BOOL APIENTRY DllMain(HMODULE /*hModule*/,
27+
DWORD ul_reason_for_call,
28+
LPVOID lpvReserved) {
29+
switch (ul_reason_for_call) {
30+
case DLL_PROCESS_ATTACH:
31+
case DLL_THREAD_ATTACH:
32+
case DLL_THREAD_DETACH:
33+
break;
34+
case DLL_PROCESS_DETACH:
35+
// Windows API doc says: "When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap memory only if the DLL is being unloaded dynamically"
36+
if (lpvReserved != nullptr) {
37+
// Process termination. Normally skipped for speed/safety,
38+
// but in leak-check builds we reclaim protobuf heap.
39+
#if defined(ONNXRUNTIME_ENABLE_MEMLEAK_CHECK)
40+
::google::protobuf::ShutdownProtobufLibrary();
41+
#endif
42+
} else {
43+
// Dynamic unload: safe to clean up.
44+
::google::protobuf::ShutdownProtobufLibrary();
45+
}
46+
break;
47+
}
48+
return TRUE;
49+
}
50+
51+
#endif // defined(_WIN32)

onnxruntime/core/providers/openvino/ov_versions/capability.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ GetCapability::GetCapability(const EPCtxHandler& ep_ctx_handler,
4242
}
4343

4444
#if OPENVINO_VERSION_MAJOR == 2025 && OPENVINO_VERSION_MINOR == 0
45-
data_ops_ = new DataOps(graph_viewer_, V_2025_0, device_type_, npu_qdq_optimizer_enabled);
45+
data_ops_ = std::make_unique<DataOps>(graph_viewer_, V_2025_0, device_type_, npu_qdq_optimizer_enabled);
4646
#elif OPENVINO_VERSION_MAJOR == 2025 && OPENVINO_VERSION_MINOR == 1
47-
data_ops_ = new DataOps(graph_viewer_, V_2025_1, device_type_, npu_qdq_optimizer_enabled);
47+
data_ops_ = std::make_unique<DataOps>(graph_viewer_, V_2025_1, device_type_, npu_qdq_optimizer_enabled);
4848
#elif OPENVINO_VERSION_MAJOR == 2025 && OPENVINO_VERSION_MINOR == 2
49-
data_ops_ = new DataOps(graph_viewer_, V_2025_2, device_type_, npu_qdq_optimizer_enabled);
49+
data_ops_ = std::make_unique<DataOps>(graph_viewer_, V_2025_2, device_type_, npu_qdq_optimizer_enabled);
5050
#else
51-
data_ops_ = new DataOps(graph_viewer_, V_2025_2, device_type_, npu_qdq_optimizer_enabled);
51+
data_ops_ = std::make_unique<DataOps>(graph_viewer_, V_2025_2, device_type_, npu_qdq_optimizer_enabled);
5252
#endif
5353
}
5454

onnxruntime/core/providers/openvino/ov_versions/capability.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class GetCapability {
1616
const EPCtxHandler& ep_ctx_handler_;
1717
const GraphViewer& graph_viewer_;
1818
std::string device_type_;
19-
DataOps* data_ops_;
19+
std::unique_ptr<DataOps> data_ops_;
2020
bool is_wholly_supported_graph_ = false;
2121
bool has_external_weights_ = false;
2222

0 commit comments

Comments
 (0)