From 7d8a8433528d782131e2adbae3541d9320f42ff8 Mon Sep 17 00:00:00 2001 From: Ryan Lai Date: Fri, 4 Mar 2022 15:47:47 -0800 Subject: [PATCH 1/4] remove local free --- onnxruntime/core/platform/windows/env.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/onnxruntime/core/platform/windows/env.cc b/onnxruntime/core/platform/windows/env.cc index 52aa9cd02afd6..3fe74d8570d7f 100644 --- a/onnxruntime/core/platform/windows/env.cc +++ b/onnxruntime/core/platform/windows/env.cc @@ -544,7 +544,8 @@ class WindowsEnv : public Env { #endif if (!*handle) { const auto error_code = GetLastError(); - LPVOID lpMsgBuf; + static const DWORD bufferLength = 64 * 1024; + std::string s(bufferLength, '\0'); FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | @@ -552,14 +553,13 @@ class WindowsEnv : public Env { NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&lpMsgBuf, + (LPWSTR)s.data(), 0, NULL); std::wostringstream oss; - oss << L"LoadLibrary failed with error " << error_code << L" \"" << (LPWSTR)lpMsgBuf << L"\" when trying to load \"" << wlibrary_filename << L"\""; + oss << L"LoadLibrary failed with error " << error_code << L" \"" << (LPWSTR)s.c_str() << L"\" when trying to load \"" << wlibrary_filename << L"\""; std::wstring errmsg = oss.str(); // TODO: trim the ending '\r' and/or '\n' common::Status status(common::ONNXRUNTIME, common::FAIL, ToUTF8String(errmsg)); - LocalFree(lpMsgBuf); return status; } return Status::OK(); @@ -577,7 +577,8 @@ class WindowsEnv : public Env { *symbol = ::GetProcAddress(reinterpret_cast(handle), symbol_name.c_str()); if (!*symbol) { const auto error_code = GetLastError(); - LPVOID lpMsgBuf; + static const DWORD bufferLength = 64 * 1024; + std::string s(bufferLength, '\0'); FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | @@ -585,14 +586,13 @@ class WindowsEnv : public Env { NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&lpMsgBuf, + (LPWSTR)s.data(), 0, NULL); std::wostringstream oss; - oss << L"Failed to find symbol " << ToWideString(symbol_name) << L" in library, error code: " << error_code << L" \"" << (LPWSTR)lpMsgBuf << L"\""; + oss << L"Failed to find symbol " << ToWideString(symbol_name) << L" in library, error code: " << error_code << L" \"" << (LPWSTR)s.c_str() << L"\""; std::wstring errmsg = oss.str(); // TODO: trim the ending '\r' and/or '\n' common::Status status(common::ONNXRUNTIME, common::FAIL, ToUTF8String(errmsg)); - LocalFree(lpMsgBuf); return status; } return Status::OK(); From 0e0e9bcdd028d1dfb86174069873f68e8e156a90 Mon Sep 17 00:00:00 2001 From: Ryan Lai Date: Mon, 7 Mar 2022 12:18:10 -0800 Subject: [PATCH 2/4] Remove local free from onnxruntime --- onnxruntime/core/platform/windows/env.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/platform/windows/env.cc b/onnxruntime/core/platform/windows/env.cc index 3fe74d8570d7f..c141284b91fd1 100644 --- a/onnxruntime/core/platform/windows/env.cc +++ b/onnxruntime/core/platform/windows/env.cc @@ -545,7 +545,7 @@ class WindowsEnv : public Env { if (!*handle) { const auto error_code = GetLastError(); static const DWORD bufferLength = 64 * 1024; - std::string s(bufferLength, '\0'); + std::wstring s(bufferLength, '\0'); FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | @@ -556,7 +556,7 @@ class WindowsEnv : public Env { (LPWSTR)s.data(), 0, NULL); std::wostringstream oss; - oss << L"LoadLibrary failed with error " << error_code << L" \"" << (LPWSTR)s.c_str() << L"\" when trying to load \"" << wlibrary_filename << L"\""; + oss << L"LoadLibrary failed with error " << error_code << L" \"" << s.c_str() << L"\" when trying to load \"" << wlibrary_filename << L"\""; std::wstring errmsg = oss.str(); // TODO: trim the ending '\r' and/or '\n' common::Status status(common::ONNXRUNTIME, common::FAIL, ToUTF8String(errmsg)); @@ -578,7 +578,7 @@ class WindowsEnv : public Env { if (!*symbol) { const auto error_code = GetLastError(); static const DWORD bufferLength = 64 * 1024; - std::string s(bufferLength, '\0'); + std::wstring s(bufferLength, '\0'); FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | @@ -589,7 +589,7 @@ class WindowsEnv : public Env { (LPWSTR)s.data(), 0, NULL); std::wostringstream oss; - oss << L"Failed to find symbol " << ToWideString(symbol_name) << L" in library, error code: " << error_code << L" \"" << (LPWSTR)s.c_str() << L"\""; + oss << L"Failed to find symbol " << ToWideString(symbol_name) << L" in library, error code: " << error_code << L" \"" << s.c_str() << L"\""; std::wstring errmsg = oss.str(); // TODO: trim the ending '\r' and/or '\n' common::Status status(common::ONNXRUNTIME, common::FAIL, ToUTF8String(errmsg)); From 66a849827d8d3da0d8fb196ffd64b6c9d620d171 Mon Sep 17 00:00:00 2001 From: Ryan Lai Date: Mon, 7 Mar 2022 14:14:28 -0800 Subject: [PATCH 3/4] don't allocate --- onnxruntime/core/platform/windows/env.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/onnxruntime/core/platform/windows/env.cc b/onnxruntime/core/platform/windows/env.cc index c141284b91fd1..38569093678b8 100644 --- a/onnxruntime/core/platform/windows/env.cc +++ b/onnxruntime/core/platform/windows/env.cc @@ -547,7 +547,6 @@ class WindowsEnv : public Env { static const DWORD bufferLength = 64 * 1024; std::wstring s(bufferLength, '\0'); FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, @@ -580,7 +579,6 @@ class WindowsEnv : public Env { static const DWORD bufferLength = 64 * 1024; std::wstring s(bufferLength, '\0'); FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, From 3f03751f706cc207522d34080c9950ad03d0d37b Mon Sep 17 00:00:00 2001 From: Ryan Lai Date: Thu, 10 Mar 2022 17:03:21 -0800 Subject: [PATCH 4/4] Change to use constexpr to satisfy CPU build warning --- onnxruntime/core/platform/windows/env.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/platform/windows/env.cc b/onnxruntime/core/platform/windows/env.cc index 38569093678b8..1a3818b758def 100644 --- a/onnxruntime/core/platform/windows/env.cc +++ b/onnxruntime/core/platform/windows/env.cc @@ -544,7 +544,7 @@ class WindowsEnv : public Env { #endif if (!*handle) { const auto error_code = GetLastError(); - static const DWORD bufferLength = 64 * 1024; + static constexpr DWORD bufferLength = 64 * 1024; std::wstring s(bufferLength, '\0'); FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM | @@ -576,7 +576,7 @@ class WindowsEnv : public Env { *symbol = ::GetProcAddress(reinterpret_cast(handle), symbol_name.c_str()); if (!*symbol) { const auto error_code = GetLastError(); - static const DWORD bufferLength = 64 * 1024; + static constexpr DWORD bufferLength = 64 * 1024; std::wstring s(bufferLength, '\0'); FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM |