From fec0a38e6a12c8674e54a29b0c3ae54fa78e86ff Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 7 Sep 2017 06:44:38 +0300 Subject: Fix possible thread structure memory leak on SDL2 if thread terminated before cleanup. --- src/gui/windows/whoisonline.cpp | 17 ++++++----------- src/net/download.cpp | 8 ++------ src/net/ea/network.cpp | 7 ++----- src/net/ipc.cpp | 4 +--- src/utils/sdl2helper.cpp | 6 ++++++ src/utils/sdl2helper.h | 2 ++ src/utils/sdlhelper.cpp | 6 ++++++ src/utils/sdlhelper.h | 2 ++ 8 files changed, 27 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/gui/windows/whoisonline.cpp b/src/gui/windows/whoisonline.cpp index 1401ec280..35d930c4c 100644 --- a/src/gui/windows/whoisonline.cpp +++ b/src/gui/windows/whoisonline.cpp @@ -175,9 +175,8 @@ WhoIsOnline::~WhoIsOnline() config.removeListeners(this); CHECKLISTENERS - if ((mThread != nullptr) && (SDL_GetThreadID(mThread) != 0u)) - SDL_WaitThread(mThread, nullptr); - + SDL::WaitThread(mThread); + mThread = nullptr; free(mMemoryBuffer); mMemoryBuffer = nullptr; @@ -628,9 +627,8 @@ void WhoIsOnline::download() else if (mWebList) { mDownloadComplete = true; - if (mThread != nullptr && SDL_GetThreadID(mThread) != 0U) - SDL_WaitThread(mThread, nullptr); - + SDL::WaitThread(mThread); + mThread = nullptr; mDownloadComplete = false; mThread = SDL::createThread(&WhoIsOnline::downloadThread, "whoisonline", this); @@ -729,11 +727,8 @@ void WhoIsOnline::action(const ActionEvent &event) mUpdateButton->setEnabled(false); // TRANSLATORS: who is online window name setCaption(_("Who Is Online - Update")); - if (mThread != nullptr && SDL_GetThreadID(mThread) != 0U) - { - SDL_WaitThread(mThread, nullptr); - mThread = nullptr; - } + SDL::WaitThread(mThread); + mThread = nullptr; mDownloadComplete = true; } } diff --git a/src/net/download.cpp b/src/net/download.cpp index 769a498b3..54aa8b217 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -110,9 +110,7 @@ Download::~Download() mHeaders = nullptr; } - int status; - if ((mThread != nullptr) && (SDL_GetThreadID(mThread) != 0u)) - SDL_WaitThread(mThread, &status); + SDL::WaitThread(mThread); mThread = nullptr; free(mError); } @@ -208,9 +206,7 @@ void Download::cancel() logger->log("Canceling download: %s", mUrl.c_str()); mOptions.cancel = 1u; - if ((mThread != nullptr) && (SDL_GetThreadID(mThread) != 0u)) - SDL_WaitThread(mThread, nullptr); - + SDL::WaitThread(mThread); mThread = nullptr; } diff --git a/src/net/ea/network.cpp b/src/net/ea/network.cpp index ad7909c7a..1ae4d2323 100644 --- a/src/net/ea/network.cpp +++ b/src/net/ea/network.cpp @@ -140,11 +140,8 @@ void Network::disconnect() BLOCK_START("Network::disconnect") mState = IDLE; - if ((mWorkerThread != nullptr) && (SDL_GetThreadID(mWorkerThread) != 0u)) - { - SDL_WaitThread(mWorkerThread, nullptr); - mWorkerThread = nullptr; - } + SDL::WaitThread(mWorkerThread); + mWorkerThread = nullptr; if (mSocket != nullptr) { diff --git a/src/net/ipc.cpp b/src/net/ipc.cpp index b3fd3da9d..407f8ca3b 100644 --- a/src/net/ipc.cpp +++ b/src/net/ipc.cpp @@ -57,9 +57,7 @@ IPC::~IPC() } SDL_DestroyMutex(mMutex); mMutex = nullptr; - int status; - if ((mThread != nullptr) && (SDL_GetThreadID(mThread) != 0u)) - SDL_WaitThread(mThread, &status); + SDL::WaitThread(mThread); mThread = nullptr; } diff --git a/src/utils/sdl2helper.cpp b/src/utils/sdl2helper.cpp index 4f99dc512..7648ccc2b 100644 --- a/src/utils/sdl2helper.cpp +++ b/src/utils/sdl2helper.cpp @@ -188,4 +188,10 @@ void SDL::initLogger() SDL2Logger::init(); } +void SDL::WaitThread(SDL_Thread *const thread) +{ + if (thread != nullptr) + SDL_WaitThread(thread, nullptr); +} + #endif // USE_SDL2 diff --git a/src/utils/sdl2helper.h b/src/utils/sdl2helper.h index 04a258014..089bbbfab 100644 --- a/src/utils/sdl2helper.h +++ b/src/utils/sdl2helper.h @@ -63,6 +63,8 @@ namespace SDL void makeCurrentContext(void *const context); void initLogger(); + + void WaitThread(SDL_Thread *const thread); } // namespace SDL #endif // USE_SDL2 diff --git a/src/utils/sdlhelper.cpp b/src/utils/sdlhelper.cpp index 821eb40a5..324779898 100644 --- a/src/utils/sdlhelper.cpp +++ b/src/utils/sdlhelper.cpp @@ -186,4 +186,10 @@ void SDL::initLogger() { } +void SDL::WaitThread(SDL_Thread *const thread) +{ + if (thread != nullptr && SDL_GetThreadID(thread) != 0u) + SDL_WaitThread(thread, nullptr); +} + #endif // USE_SDL2 diff --git a/src/utils/sdlhelper.h b/src/utils/sdlhelper.h index 5e8c31e15..abb3934cb 100644 --- a/src/utils/sdlhelper.h +++ b/src/utils/sdlhelper.h @@ -69,6 +69,8 @@ namespace SDL void makeCurrentContext(void *const context); void initLogger(); + + void WaitThread(SDL_Thread *const thread); } // namespace SDL #endif // USE_SDL2 -- cgit v1.2.3-60-g2f50