Remove the reference to the thread when it is detached

Fixes https://github.com/libsdl-org/SDL/issues/12301
This commit is contained in:
Sam Lantinga
2025-02-16 07:55:58 -08:00
parent 831fc70923
commit e3d9f1172c

View File

@@ -334,7 +334,6 @@ void SDL_RunThread(SDL_Thread *thread)
if (!SDL_CompareAndSwapAtomicInt(&thread->state, SDL_THREAD_ALIVE, SDL_THREAD_COMPLETE)) { if (!SDL_CompareAndSwapAtomicInt(&thread->state, SDL_THREAD_ALIVE, SDL_THREAD_COMPLETE)) {
// Clean up if something already detached us. // Clean up if something already detached us.
if (SDL_GetThreadState(thread) == SDL_THREAD_DETACHED) { 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->name); // Can't free later, we've already cleaned up TLS
SDL_free(thread); SDL_free(thread);
} }
@@ -457,7 +456,7 @@ bool SDL_SetCurrentThreadPriority(SDL_ThreadPriority priority)
void SDL_WaitThread(SDL_Thread *thread, int *status) void SDL_WaitThread(SDL_Thread *thread, int *status)
{ {
if (!ThreadValid(thread) || SDL_GetThreadState(thread) == SDL_THREAD_DETACHED) { if (!ThreadValid(thread)) {
if (status) { if (status) {
*status = -1; *status = -1;
} }
@@ -488,6 +487,9 @@ void SDL_DetachThread(SDL_Thread *thread)
return; 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. // Grab dibs if the state is alive+joinable.
if (SDL_CompareAndSwapAtomicInt(&thread->state, SDL_THREAD_ALIVE, SDL_THREAD_DETACHED)) { if (SDL_CompareAndSwapAtomicInt(&thread->state, SDL_THREAD_ALIVE, SDL_THREAD_DETACHED)) {
SDL_SYS_DetachThread(thread); SDL_SYS_DetachThread(thread);