From 20dbe907714730a7d7ab1957beafbfb608e20320 Mon Sep 17 00:00:00 2001 From: Edoardo Lolletti Date: Mon, 24 Jun 2024 19:45:37 +0200 Subject: [PATCH] Fix std::thread memory leak In the stdcpp thread implementation, the allocated std::thread objects were never deleted after joining/detaching --- src/thread/stdcpp/SDL_systhread.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/thread/stdcpp/SDL_systhread.cpp b/src/thread/stdcpp/SDL_systhread.cpp index 06bc933ed9..350431f0ed 100644 --- a/src/thread/stdcpp/SDL_systhread.cpp +++ b/src/thread/stdcpp/SDL_systhread.cpp @@ -121,8 +121,12 @@ SDL_SYS_WaitThread(SDL_Thread *thread) try { std::thread *cpp_thread = (std::thread *)thread->handle; - if (cpp_thread->joinable()) { - cpp_thread->join(); + if (cpp_thread) { + if (cpp_thread->joinable()) { + cpp_thread->join(); + } + delete cpp_thread; + thread->handle = nullptr; } } catch (std::system_error &) { // An error occurred when joining the thread. SDL_WaitThread does not, @@ -140,8 +144,12 @@ SDL_SYS_DetachThread(SDL_Thread *thread) try { std::thread *cpp_thread = (std::thread *)thread->handle; - if (cpp_thread->joinable()) { - cpp_thread->detach(); + if (cpp_thread) { + if (cpp_thread->joinable()) { + cpp_thread->detach(); + } + delete cpp_thread; + thread->handle = nullptr; } } catch (std::system_error &) { // An error occurred when detaching the thread. SDL_DetachThread does not,