ShutdownProtobufLibrary when unload paddle frontend dynmaic library t… (#9442)

* ShutdownProtobufLibrary when unload paddle frontend dynmaic library to fix probuf memory leak

* ShutdownProtobufLibrary if the frontend libraries use protobuf

* make shutdown_protobuf a library
This commit is contained in:
mei, yang 2022-01-12 18:07:51 +08:00 committed by GitHub
parent 84bf5fa178
commit 613facafde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 1 deletions

View File

@ -89,7 +89,7 @@ unset(protobuf_installed CACHE)
# [LINK_LIBRARIES <lib1 lib2 ...>])
#
macro(ov_add_frontend)
set(options LINKABLE_FRONTEND PROTOBUF_LITE SKIP_NCC_STYLE SKIP_INSTALL)
set(options LINKABLE_FRONTEND SHUTDOWN_PROTOBUF PROTOBUF_LITE SKIP_NCC_STYLE SKIP_INSTALL)
set(oneValueArgs NAME FILEDESCRIPTION)
set(multiValueArgs LINK_LIBRARIES PROTO_FILES)
cmake_parse_arguments(OV_FRONTEND "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
@ -149,6 +149,11 @@ macro(ov_add_frontend)
add_library(openvino::frontend::${OV_FRONTEND_NAME} ALIAS ${TARGET_NAME})
endif()
# Shutdown protobuf when unloading the front dynamic library
if(OV_FRONTEND_SHUTDOWN_PROTOBUF AND BUILD_SHARED_LIBS)
target_link_libraries(${TARGET_NAME} PRIVATE ov_protobuf_shutdown)
endif()
if(NOT BUILD_SHARED_LIBS)
# override default function names
target_compile_definitions(${TARGET_NAME} PRIVATE

View File

@ -72,3 +72,7 @@ openvino_developer_export_targets(COMPONENT core_legacy TARGETS ${TARGET_NAME})
install(DIRECTORY ${FRONTEND_INCLUDE_DIR}/openvino
DESTINATION ${FRONTEND_INSTALL_INCLUDE}
COMPONENT core_dev)
# Shutdown protobuf library
add_subdirectory(shutdown_protobuf)

View File

@ -0,0 +1,4 @@
set(TARGET_NAME ov_protobuf_shutdown)
add_library(${TARGET_NAME} STATIC shutdown_protobuf.cpp)
target_sources(${TARGET_NAME} INTERFACE shutdown_protobuf.cpp)
target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${Protobuf_INCLUDE_DIRS})

View File

@ -0,0 +1,27 @@
#include <google/protobuf/stubs/common.h>
#if defined(_WIN32)
# 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;
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() {
google::protobuf::ShutdownProtobufLibrary();
}
#endif

View File

@ -4,6 +4,7 @@
ov_add_frontend(NAME onnx
LINKABLE_FRONTEND
SHUTDOWN_PROTOBUF
PROTOBUF_LITE
SKIP_NCC_STYLE
FILEDESCRIPTION "FrontEnd to load and convert ONNX file format"

View File

@ -4,6 +4,7 @@
ov_add_frontend(NAME paddle
LINKABLE_FRONTEND
SHUTDOWN_PROTOBUF
PROTOBUF_LITE
FILEDESCRIPTION "FrontEnd to load and convert PaddlePaddle file format"
LINK_LIBRARIES openvino::runtime::dev)

View File

@ -4,6 +4,7 @@
# TODO: Add LINKABLE_FRONTEND option when tensorflow frontend directory is moved to openvino folder
ov_add_frontend(NAME tensorflow
SHUTDOWN_PROTOBUF
SKIP_INSTALL
FILEDESCRIPTION "FrontEnd to load and convert TensorFlow file format"
LINK_LIBRARIES openvino::util)