Browse Source

Fix std::thread memory leak

In the stdcpp thread implementation, the allocated std::thread objects were never deleted after joining/detaching

(cherry picked from commit 20dbe907714730a7d7ab1957beafbfb608e20320)
(cherry picked from commit 99d7b9e6268d2037967090947b1ee77b2cb2a2c0)
Edoardo Lolletti 9 months ago
parent
commit
c18173c260
1 changed files with 12 additions and 4 deletions
  1. 12 4
      src/thread/stdcpp/SDL_systhread.cpp

+ 12 - 4
src/thread/stdcpp/SDL_systhread.cpp

@@ -120,8 +120,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,
@@ -139,8 +143,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,