From 0dca414e02ffa1249488a21e7d048e061e83c3b1 Mon Sep 17 00:00:00 2001 From: Deve Date: Sat, 22 Sep 2018 22:06:27 +0200 Subject: [PATCH] Fixed some memory leaks when a server is closed --- src/main.cpp | 2 +- src/main_loop.cpp | 30 +++++++++++++++++------------- src/main_loop.hpp | 6 +++++- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f65168e69..3d517ace4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1841,7 +1841,7 @@ void main_abort() { if (main_loop) { - main_loop->abort(); + main_loop->requestAbort(); } } #ifdef ANDROID diff --git a/src/main_loop.cpp b/src/main_loop.cpp index 73238eaa2..635816eaa 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -65,7 +65,8 @@ LRESULT CALLBACK separateProcessProc(_In_ HWND hwnd, _In_ UINT uMsg, // ---------------------------------------------------------------------------- MainLoop::MainLoop(unsigned parent_pid) - : m_abort(false), m_ticks_adjustment(0), m_parent_pid(parent_pid) + : m_abort(false), m_request_abort(false), m_ticks_adjustment(0), + m_parent_pid(parent_pid) { m_curr_time = 0; m_prev_time = 0; @@ -327,14 +328,20 @@ void MainLoop::run() // Shutdown next frame if shutdown request is sent while loading the // world - if (STKHost::existHost() && STKHost::get()->requestedShutdown()) + if (STKHost::existHost() && + (STKHost::get()->requestedShutdown() || m_request_abort)) { - SFXManager::get()->quickSound("anvil"); core::stringw msg = _("Server connection timed out."); - if (!STKHost::get()->getErrorMessage().empty()) + + if (!ProfileWorld::isNoGraphics()) { - msg = STKHost::get()->getErrorMessage(); + SFXManager::get()->quickSound("anvil"); + if (!STKHost::get()->getErrorMessage().empty()) + { + msg = STKHost::get()->getErrorMessage(); + } } + STKHost::get()->shutdown(); // In case the user opened a race pause dialog GUIEngine::ModalDialog::dismiss(); @@ -364,6 +371,11 @@ void MainLoop::run() } NetworkConfig::get()->unsetNetworking(); } + + if (m_request_abort) + { + m_abort = true; + } if (!m_abort) { @@ -482,12 +494,4 @@ void MainLoop::run() } // run -//----------------------------------------------------------------------------- -/** Set the abort flag, causing the mainloop to be left. - */ -void MainLoop::abort() -{ - m_abort = true; -} // abort - /* EOF */ diff --git a/src/main_loop.hpp b/src/main_loop.hpp index efa4c332e..ce60eda8a 100644 --- a/src/main_loop.hpp +++ b/src/main_loop.hpp @@ -31,6 +31,8 @@ class MainLoop private: /** True if the main loop should exit. */ std::atomic_bool m_abort; + + std::atomic_bool m_request_abort; /** True if the frame rate should be throttled. */ bool m_throttle_fps; @@ -48,7 +50,9 @@ public: MainLoop(unsigned parent_pid); ~MainLoop(); void run(); - void abort(); + /** Set the abort flag, causing the mainloop to be left. */ + void abort() { m_abort = true; } + void requestAbort() { m_request_abort = true; } void setThrottleFPS(bool throttle) { m_throttle_fps = throttle; } // ------------------------------------------------------------------------ /** Returns true if STK is to be stoppe. */