From 403dc7645918c0f229ddb8656c1d48956ae3f534 Mon Sep 17 00:00:00 2001 From: Kaito Udagawa Date: Thu, 18 Jan 2024 12:44:21 +0900 Subject: [PATCH] Improve the handling of path string (#528) * Fix uninitialized string bug * Update background-filter.cpp * Update ort-session-utils.cpp * Update ort-session-utils.cpp * Update ort-session-utils.cpp * Update ort-session-utils.cpp * Update ort-session-utils.cpp * Update ort-session-utils.cpp --- src/FilterData.h | 4 ++-- src/background-filter.cpp | 4 ++-- src/ort-utils/ort-session-utils.cpp | 19 +++++++++++-------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/FilterData.h b/src/FilterData.h index a8e22e3b..6e2d0cae 100644 --- a/src/FilterData.h +++ b/src/FilterData.h @@ -30,9 +30,9 @@ struct filter_data : public ORTModelData { std::mutex outputLock; #if _WIN32 - const wchar_t *modelFilepath = nullptr; + std::wstring modelFilepath; #else - const char *modelFilepath = nullptr; + std::string modelFilepath; #endif }; diff --git a/src/background-filter.cpp b/src/background-filter.cpp index 873981fc..3c59d3cb 100644 --- a/src/background-filter.cpp +++ b/src/background-filter.cpp @@ -367,9 +367,9 @@ void background_filter_update(void *data, obs_data_t *settings) obs_log(LOG_INFO, " Blur Focus Depth: %f", tf->blurFocusDepth); obs_log(LOG_INFO, " Disabled: %s", tf->isDisabled ? "true" : "false"); #ifdef _WIN32 - obs_log(LOG_INFO, " Model file path: %S", tf->modelFilepath); + obs_log(LOG_INFO, " Model file path: %S", tf->modelFilepath.c_str()); #else - obs_log(LOG_INFO, " Model file path: %s", tf->modelFilepath); + obs_log(LOG_INFO, " Model file path: %s", tf->modelFilepath.c_str()); #endif } diff --git a/src/ort-utils/ort-session-utils.cpp b/src/ort-utils/ort-session-utils.cpp index fd83aa39..795e945e 100644 --- a/src/ort-utils/ort-session-utils.cpp +++ b/src/ort-utils/ort-session-utils.cpp @@ -13,6 +13,7 @@ #ifdef _WIN32 #include #include +#include #endif // _WIN32 #include @@ -51,17 +52,19 @@ int createOrtSession(filter_data *tf) } std::string modelFilepath_s(modelFilepath_rawPtr); - bfree(modelFilepath_rawPtr); #if _WIN32 - std::wstring modelFilepath_ws(modelFilepath_s.size(), L' '); - std::copy(modelFilepath_s.begin(), modelFilepath_s.end(), - modelFilepath_ws.begin()); - tf->modelFilepath = modelFilepath_ws.c_str(); + int outLength = MultiByteToWideChar( + CP_ACP, MB_PRECOMPOSED, modelFilepath_rawPtr, -1, nullptr, 0); + tf->modelFilepath = std::wstring(outLength, L'\0'); + MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, modelFilepath_rawPtr, -1, + tf->modelFilepath.data(), outLength); #else - tf->modelFilepath = modelFilepath_s.c_str(); + tf->modelFilepath = std::string(modelFilepath_rawPtr); #endif + bfree(modelFilepath_rawPtr); + try { #if defined(__linux__) && defined(__x86_64__) && \ !defined(DISABLE_ONNXRUNTIME_GPU) @@ -92,8 +95,8 @@ int createOrtSession(filter_data *tf) sessionOptions, coreml_flags)); } #endif - tf->session.reset(new Ort::Session(*tf->env, tf->modelFilepath, - sessionOptions)); + tf->session.reset(new Ort::Session( + *tf->env, tf->modelFilepath.c_str(), sessionOptions)); } catch (const std::exception &e) { obs_log(LOG_ERROR, "%s", e.what()); return OBS_BGREMOVAL_ORT_SESSION_ERROR_STARTUP;