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 20dbe90771)
This commit is contained in:
Edoardo Lolletti
2024-06-24 19:45:37 +02:00
committed by Sam Lantinga
parent b9f005505b
commit 99d7b9e626
+8
View File
@@ -120,9 +120,13 @@ SDL_SYS_WaitThread(SDL_Thread *thread)
try { try {
std::thread *cpp_thread = (std::thread *)thread->handle; std::thread *cpp_thread = (std::thread *)thread->handle;
if (cpp_thread) {
if (cpp_thread->joinable()) { if (cpp_thread->joinable()) {
cpp_thread->join(); cpp_thread->join();
} }
delete cpp_thread;
thread->handle = nullptr;
}
} catch (std::system_error &) { } catch (std::system_error &) {
// An error occurred when joining the thread. SDL_WaitThread does not, // An error occurred when joining the thread. SDL_WaitThread does not,
// however, seem to provide a means to report errors to its callers // however, seem to provide a means to report errors to its callers
@@ -139,9 +143,13 @@ SDL_SYS_DetachThread(SDL_Thread *thread)
try { try {
std::thread *cpp_thread = (std::thread *)thread->handle; std::thread *cpp_thread = (std::thread *)thread->handle;
if (cpp_thread) {
if (cpp_thread->joinable()) { if (cpp_thread->joinable()) {
cpp_thread->detach(); cpp_thread->detach();
} }
delete cpp_thread;
thread->handle = nullptr;
}
} catch (std::system_error &) { } catch (std::system_error &) {
// An error occurred when detaching the thread. SDL_DetachThread does not, // An error occurred when detaching the thread. SDL_DetachThread does not,
// however, seem to provide a means to report errors to its callers // however, seem to provide a means to report errors to its callers