From e3d9f1172c7eeb4fadddc636853b4e86c3609476 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 16 Feb 2025 07:55:58 -0800 Subject: [PATCH] Remove the reference to the thread when it is detached Fixes https://github.com/libsdl-org/SDL/issues/12301 --- src/thread/SDL_thread.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/thread/SDL_thread.c b/src/thread/SDL_thread.c index ee9f9bbb56..99cb9dd7f1 100644 --- a/src/thread/SDL_thread.c +++ b/src/thread/SDL_thread.c @@ -334,7 +334,6 @@ void SDL_RunThread(SDL_Thread *thread) if (!SDL_CompareAndSwapAtomicInt(&thread->state, SDL_THREAD_ALIVE, SDL_THREAD_COMPLETE)) { // Clean up if something already detached us. if (SDL_GetThreadState(thread) == SDL_THREAD_DETACHED) { - SDL_SetObjectValid(thread, SDL_OBJECT_TYPE_THREAD, false); SDL_free(thread->name); // Can't free later, we've already cleaned up TLS SDL_free(thread); } @@ -457,7 +456,7 @@ bool SDL_SetCurrentThreadPriority(SDL_ThreadPriority priority) void SDL_WaitThread(SDL_Thread *thread, int *status) { - if (!ThreadValid(thread) || SDL_GetThreadState(thread) == SDL_THREAD_DETACHED) { + if (!ThreadValid(thread)) { if (status) { *status = -1; } @@ -488,6 +487,9 @@ void SDL_DetachThread(SDL_Thread *thread) return; } + // The thread may vanish at any time, it's no longer valid + SDL_SetObjectValid(thread, SDL_OBJECT_TYPE_THREAD, false); + // Grab dibs if the state is alive+joinable. if (SDL_CompareAndSwapAtomicInt(&thread->state, SDL_THREAD_ALIVE, SDL_THREAD_DETACHED)) { SDL_SYS_DetachThread(thread);