Browse Source

Remove the reference to the thread when it is detached

Fixes https://github.com/libsdl-org/SDL/issues/12301
Sam Lantinga 2 months ago
parent
commit
e3d9f1172c
1 changed files with 4 additions and 2 deletions
  1. 4 2
      src/thread/SDL_thread.c

+ 4 - 2
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);