Skip to content

Commit

Permalink
Fix std::thread memory leak
Browse files Browse the repository at this point in the history
In the stdcpp thread implementation, the allocated std::thread objects were never deleted after joining/detaching

(cherry picked from commit 20dbe90)
  • Loading branch information
edo9300 authored and slouken committed Jun 24, 2024
1 parent b9f0055 commit 99d7b9e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/thread/stdcpp/SDL_systhread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 99d7b9e

Please sign in to comment.