From afc1e765759e61804e94d590029b733b51822d13 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 4 May 2025 11:18:39 +0200 Subject: [PATCH] Codefix: StartNewThread uses char* after returning --- src/os/macosx/macos.h | 2 +- src/os/macosx/macos.mm | 6 +++--- src/os/unix/unix.cpp | 6 +++--- src/os/windows/win32.cpp | 6 +++--- src/os/windows/win32.h | 1 - src/thread.h | 8 ++++---- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/os/macosx/macos.h b/src/os/macosx/macos.h index 2bda5509e1..5c9e6a52a9 100644 --- a/src/os/macosx/macos.h +++ b/src/os/macosx/macos.h @@ -36,7 +36,7 @@ inline bool MacOSVersionIsAtLeast(long major, long minor, long bugfix) bool IsMonospaceFont(CFStringRef name); -void MacOSSetThreadName(const char *name); +void MacOSSetThreadName(const std::string &name); uint64_t MacOSGetPhysicalMemory(); diff --git a/src/os/macosx/macos.mm b/src/os/macosx/macos.mm index 052357b8d2..3ad91412e7 100644 --- a/src/os/macosx/macos.mm +++ b/src/os/macosx/macos.mm @@ -242,15 +242,15 @@ bool IsMonospaceFont(CFStringRef name) * Set the name of the current thread for the debugger. * @param name The new name of the current thread. */ -void MacOSSetThreadName(const char *name) +void MacOSSetThreadName(const std::string &name) { if (MacOSVersionIsAtLeast(10, 6, 0)) { - pthread_setname_np(name); + pthread_setname_np(name.c_str()); } NSThread *cur = [ NSThread currentThread ]; if (cur != nil && [ cur respondsToSelector:@selector(setName:) ]) { - [ cur performSelector:@selector(setName:) withObject:[ NSString stringWithUTF8String:name ] ]; + [ cur performSelector:@selector(setName:) withObject:[ NSString stringWithUTF8String:name.c_str() ] ]; } } diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 8ccdb30a30..5c51267bf0 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -243,12 +243,12 @@ void OSOpenBrowser(const std::string &url) } #endif /* __APPLE__ */ -void SetCurrentThreadName([[maybe_unused]] const char *threadName) +void SetCurrentThreadName([[maybe_unused]] const std::string &thread_name) { #if defined(__GLIBC__) - if (threadName) pthread_setname_np(pthread_self(), threadName); + pthread_setname_np(pthread_self(), thread_name.c_str()); #endif /* defined(__GLIBC__) */ #if defined(__APPLE__) - MacOSSetThreadName(threadName); + MacOSSetThreadName(thread_name); #endif /* defined(__APPLE__) */ } diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 27e9f4943f..451d635957 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -520,11 +520,11 @@ PACK_N(struct THREADNAME_INFO { /** * Signal thread name to any attached debuggers. */ -void SetCurrentThreadName(const char *threadName) +void SetCurrentThreadName(const std::string &thread_name) { THREADNAME_INFO info; info.dwType = 0x1000; - info.szName = threadName; + info.szName = thread_name.c_str(); info.dwThreadID = -1; info.dwFlags = 0; @@ -537,5 +537,5 @@ void SetCurrentThreadName(const char *threadName) #pragma warning(pop) } #else -void SetCurrentThreadName(const char *) {} +void SetCurrentThreadName(const std::string &) {} #endif diff --git a/src/os/windows/win32.h b/src/os/windows/win32.h index 71ad04fd79..e11a0089a0 100644 --- a/src/os/windows/win32.h +++ b/src/os/windows/win32.h @@ -15,7 +15,6 @@ bool MyShowCursor(bool show, bool toggle = false); char *convert_from_fs(const std::wstring_view src, std::span dst_buf); wchar_t *convert_to_fs(std::string_view src, std::span dst_buf); -void Win32SetCurrentLocaleName(const char *iso_code); int OTTDStringCompare(std::string_view s1, std::string_view s2); int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive); diff --git a/src/thread.h b/src/thread.h index 55b883a043..dd9177fff3 100644 --- a/src/thread.h +++ b/src/thread.h @@ -30,7 +30,7 @@ inline void CSleep(int milliseconds) * Name the thread this function is called on for the debugger. * @param name Name to set for the thread.. */ -void SetCurrentThreadName(const char *name); +void SetCurrentThreadName(const std::string &name); /** @@ -44,13 +44,13 @@ void SetCurrentThreadName(const char *name); * @return True if the thread was successfully started, false otherwise. */ template -inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs&&... _Ax) +inline bool StartNewThread(std::thread *thr, std::string_view name, TFn&& _Fx, TArgs&&... _Ax) { try { static std::mutex thread_startup_mutex; std::lock_guard lock(thread_startup_mutex); - std::thread t([] (const char *name, TFn&& F, TArgs&&... A) { + std::thread t([] (std::string name, TFn&& F, TArgs&&... A) { /* Delay starting the thread till the main thread is finished * with the administration. This prevent race-conditions on * startup. */ @@ -68,7 +68,7 @@ inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs& } catch (...) { NOT_REACHED(); } - }, name, std::forward(_Fx), std::forward(_Ax)...); + }, std::string{name}, std::forward(_Fx), std::forward(_Ax)...); if (thr != nullptr) { *thr = std::move(t);