From 1eb20ad5aa84654b65c77425ee0aed9fdb042cb9 Mon Sep 17 00:00:00 2001 From: jpark37 Date: Mon, 30 Aug 2021 03:07:21 -0700 Subject: [PATCH] libobs/util: Improve SetThreadDescription usage Fix warning about potential passing of NULL to FreeLibrary, and switch from Kernel32.dll to KernelBase.dll based on MS documentation. --- libobs/util/threading-windows.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/libobs/util/threading-windows.c b/libobs/util/threading-windows.c index 72b5792a2..8c50b4adb 100644 --- a/libobs/util/threading-windows.c +++ b/libobs/util/threading-windows.c @@ -194,20 +194,23 @@ void os_set_thread_name(const char *name) } #endif - typedef HRESULT(WINAPI * set_thread_description_t)(HANDLE thread, - PCWSTR desc); + const HMODULE hModule = LoadLibrary(L"KernelBase.dll"); + if (hModule) { + typedef HRESULT(WINAPI * set_thread_description_t)(HANDLE, + PCWSTR); - HMODULE k32 = LoadLibraryW(L"Kernel32.dll"); - set_thread_description_t std = NULL; - std = (set_thread_description_t)GetProcAddress(k32, - "SetThreadDescription"); - if (std) { - wchar_t *wname; - os_utf8_to_wcs_ptr(name, 0, &wname); + const set_thread_description_t std = + (set_thread_description_t)GetProcAddress( + hModule, "SetThreadDescription"); + if (std) { + wchar_t *wname; + os_utf8_to_wcs_ptr(name, 0, &wname); - std(GetCurrentThread(), wname); + std(GetCurrentThread(), wname); - bfree(wname); + bfree(wname); + } + + FreeLibrary(hModule); } - FreeLibrary(k32); }