diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 8f276cca8..4e6193757 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -3049,19 +3049,6 @@ void CClient::Run() g_UuidManager.DebugDump(); } - // init SDL - { - if(SDL_Init(0) < 0) - { - dbg_msg("client", "unable to init SDL base: %s", SDL_GetError()); - return; - } - -#ifndef CONF_PLATFORM_ANDROID - atexit(SDL_Quit); -#endif - } - // init graphics { m_pGraphics = CreateEngineGraphicsThreaded(); @@ -3492,11 +3479,6 @@ void CClient::Run() m_aNetClient[i].Close(); delete m_pEditor; - m_pInput->Shutdown(); - m_pGraphics->Shutdown(); - - // shutdown SDL - SDL_Quit(); } bool CClient::CtrlShiftKey(int Key, bool &Last) @@ -4788,6 +4770,17 @@ int main(int argc, const char **argv) } } + // init SDL + if(SDL_Init(0) < 0) + { + dbg_msg("client", "unable to init SDL base: %s", SDL_GetError()); + return -1; + } + +#ifndef CONF_PLATFORM_ANDROID + atexit(SDL_Quit); +#endif + // run the client dbg_msg("client", "starting..."); pClient->Run(); @@ -4806,8 +4799,12 @@ int main(int argc, const char **argv) shell_execute(pStorage->GetBinaryPath(PLAT_CLIENT_EXEC, aBuf, sizeof aBuf)); } + pKernel->Shutdown(); delete pKernel; + // shutdown SDL + SDL_Quit(); + #ifdef CONF_PLATFORM_ANDROID // properly close this native thread, so globals are destructed std::exit(0); diff --git a/src/engine/client/sound.cpp b/src/engine/client/sound.cpp index 617316df9..28bc3c0d7 100644 --- a/src/engine/client/sound.cpp +++ b/src/engine/client/sound.cpp @@ -342,7 +342,7 @@ int CSound::Update() return 0; } -int CSound::Shutdown() +void CSound::Shutdown() { for(unsigned SampleID = 0; SampleID < NUM_SAMPLES; SampleID++) { @@ -353,7 +353,6 @@ int CSound::Shutdown() SDL_QuitSubSystem(SDL_INIT_AUDIO); free(m_pMixBuffer); m_pMixBuffer = 0; - return 0; } int CSound::AllocID() diff --git a/src/engine/client/sound.h b/src/engine/client/sound.h index c259bdde0..d082bca22 100644 --- a/src/engine/client/sound.h +++ b/src/engine/client/sound.h @@ -30,7 +30,7 @@ class CSound : public IEngineSound public: int Init() override; int Update() override; - int Shutdown() override; + void Shutdown() override; bool IsSoundEnabled() override { return m_SoundEnabled; } diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 9f0e6d68f..01fccce7f 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -524,7 +524,7 @@ class IEngineGraphics : public IGraphics MACRO_INTERFACE("enginegraphics", 0) public: virtual int Init() = 0; - virtual void Shutdown() = 0; + virtual void Shutdown() override = 0; virtual void Minimize() = 0; virtual void Maximize() = 0; diff --git a/src/engine/input.h b/src/engine/input.h index 546a6a502..0368e7306 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -128,7 +128,7 @@ class IEngineInput : public IInput MACRO_INTERFACE("engineinput", 0) public: virtual void Init() = 0; - virtual void Shutdown() = 0; + virtual void Shutdown() override = 0; virtual int Update() = 0; virtual int VideoRestartNeeded() = 0; }; diff --git a/src/engine/kernel.h b/src/engine/kernel.h index 37d23836f..400a4167b 100644 --- a/src/engine/kernel.h +++ b/src/engine/kernel.h @@ -20,6 +20,7 @@ protected: public: IInterface() : m_pKernel(nullptr) {} + virtual void Shutdown() {} virtual ~IInterface() {} }; @@ -40,6 +41,7 @@ class IKernel public: static IKernel *Create(); + virtual void Shutdown() = 0; virtual ~IKernel() {} // templated access to handle pointer conversions and interface names diff --git a/src/engine/shared/kernel.cpp b/src/engine/shared/kernel.cpp index 00e3e8357..7737c7823 100644 --- a/src/engine/shared/kernel.cpp +++ b/src/engine/shared/kernel.cpp @@ -44,6 +44,15 @@ public: m_NumInterfaces = 0; } + void Shutdown() override + { + for(int i = m_NumInterfaces - 1; i >= 0; i--) + { + if(m_aInterfaces[i].m_AutoDestroy) + m_aInterfaces[i].m_pInterface->Shutdown(); + } + } + virtual ~CKernel() { // delete interfaces in reverse order just the way it would happen to objects on the stack diff --git a/src/engine/sound.h b/src/engine/sound.h index 559abaa23..79a73cdbd 100644 --- a/src/engine/sound.h +++ b/src/engine/sound.h @@ -110,7 +110,7 @@ class IEngineSound : public ISound public: virtual int Init() = 0; virtual int Update() = 0; - virtual int Shutdown() = 0; + virtual void Shutdown() override = 0; }; extern IEngineSound *CreateEngineSound();