From b13b5142f1009a71ff06786ac8c8db92891f566a Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 1 Oct 2024 21:03:44 +0200 Subject: Core/Utilities: Extend make_unique_ptr_with_deleter functionality to allow it to create deleters with compile time constant functions (reduces its size to just sizeof(void*)) --- .../Debugging/Windows/WheatyExceptionReport.cpp | 57 +++++++++++----------- 1 file changed, 28 insertions(+), 29 deletions(-) (limited to 'src/common/Debugging') diff --git a/src/common/Debugging/Windows/WheatyExceptionReport.cpp b/src/common/Debugging/Windows/WheatyExceptionReport.cpp index 5cb29c56aae..8bdce95aee5 100644 --- a/src/common/Debugging/Windows/WheatyExceptionReport.cpp +++ b/src/common/Debugging/Windows/WheatyExceptionReport.cpp @@ -6,6 +6,7 @@ #include "WheatyExceptionReport.h" #include "Errors.h" #include "GitRevision.h" +#include "Memory.h" #include #include #include @@ -411,6 +412,12 @@ BOOL WheatyExceptionReport::_GetWindowsVersion(TCHAR* szVersion, DWORD cntMax) return TRUE; } +template T> +using com_unique_ptr_deleter = decltype(Trinity::unique_ptr_deleterRelease(); }>()); + +template T> +using com_unique_ptr = std::unique_ptr>; + BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cntMax) { // Step 1: -------------------------------------------------- @@ -419,10 +426,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn if (FAILED(hres)) return FALSE; - std::shared_ptr com(nullptr, [](void*) - { - CoUninitialize(); - }); + auto com = Trinity::make_unique_ptr_with_deleter<[](void*) { CoUninitialize(); }>(&hres); // Step 2: -------------------------------------------------- // Set general COM security levels -------------------------- @@ -443,7 +447,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Step 3: --------------------------------------------------- // Obtain the initial locator to WMI ------------------------- - std::shared_ptr loc = []() -> std::shared_ptr + com_unique_ptr loc([] { IWbemLocator* tmp = nullptr; HRESULT hres = CoCreateInstance( @@ -453,11 +457,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn IID_IWbemLocator, reinterpret_cast(&tmp)); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IWbemLocator* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); if (!loc) return FALSE; @@ -466,7 +467,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Connect to the root\cimv2 namespace with // the current user and obtain pointer pSvc // to make IWbemServices calls. - std::shared_ptr svc = [loc]() ->std::shared_ptr + com_unique_ptr svc([&] { IWbemServices* tmp = nullptr; HRESULT hres = loc->ConnectServer( @@ -480,11 +481,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn &tmp // pointer to IWbemServices proxy ); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IWbemServices* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); if (!svc) return FALSE; @@ -509,7 +507,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Use the IWbemServices pointer to make requests of WMI ---- // For example, get the name of the operating system - std::shared_ptr queryResult = [svc]() -> std::shared_ptr + com_unique_ptr queryResult([&] { IEnumWbemClassObject* tmp = nullptr; HRESULT hres = svc->ExecQuery( @@ -519,11 +517,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn nullptr, &tmp); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IEnumWbemClassObject* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); BOOL result = FALSE; // Step 7: ------------------------------------------------- @@ -532,11 +527,17 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn { do { - IWbemClassObject* fields = nullptr; - - ULONG rows = 0; - queryResult->Next(WBEM_INFINITE, 1, &fields, &rows); - if (!rows) + auto [fields, rows] = [&] + { + IWbemClassObject* fields = nullptr; + ULONG rows = 0; + HRESULT hres = queryResult->Next(WBEM_INFINITE, 1, &fields, &rows); + return SUCCEEDED(hres) && rows + ? std::pair(com_unique_ptr(fields), rows) + : std::pair(com_unique_ptr(), ULONG(0)); + }(); + + if (!fields || !rows) break; VARIANT field; @@ -558,8 +559,6 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn } VariantClear(&field); - fields->Release(); - result = TRUE; } while (true); } -- cgit v1.2.3