From 46f921fb95b313c809a6c19e0c1407907e008106 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 22 Mar 2020 12:37:11 +1000 Subject: [PATCH] SDLControllerInterface: Fix crash on shutdown --- .../sdl_controller_interface.cpp | 19 +++++++------------ .../sdl_controller_interface.h | 3 +-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/frontend-common/sdl_controller_interface.cpp b/src/frontend-common/sdl_controller_interface.cpp index 9b1e6a354..99938e1cd 100644 --- a/src/frontend-common/sdl_controller_interface.cpp +++ b/src/frontend-common/sdl_controller_interface.cpp @@ -36,16 +36,16 @@ bool SDLControllerInterface::Initialize(CommonHostInterface* host_interface) void SDLControllerInterface::Shutdown() { - ControllerInterface::Shutdown(); - while (!m_controllers.empty()) - CloseGameController(m_controllers.begin()->joystick_id); + CloseGameController(m_controllers.begin()->joystick_id, false); if (m_sdl_subsystem_initialized) { SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC); m_sdl_subsystem_initialized = false; } + + ControllerInterface::Shutdown(); } void SDLControllerInterface::PollEvents() @@ -74,7 +74,7 @@ bool SDLControllerInterface::ProcessSDLEvent(const SDL_Event* event) case SDL_CONTROLLERDEVICEREMOVED: { Log_InfoPrintf("Controller %d removed", event->cdevice.which); - CloseGameController(event->cdevice.which); + CloseGameController(event->cdevice.which, true); return true; } @@ -153,13 +153,7 @@ bool SDLControllerInterface::OpenGameController(int index) return true; } -void SDLControllerInterface::CloseGameControllers() -{ - while (!m_controllers.empty()) - CloseGameController(m_controllers.begin()->player_id); -} - -bool SDLControllerInterface::CloseGameController(int joystick_index) +bool SDLControllerInterface::CloseGameController(int joystick_index, bool notify) { auto it = GetControllerDataForJoystickId(joystick_index); if (it == m_controllers.end()) @@ -173,7 +167,8 @@ bool SDLControllerInterface::CloseGameController(int joystick_index) SDL_GameControllerClose(static_cast(it->controller)); m_controllers.erase(it); - OnControllerDisconnected(player_index); + if (notify) + OnControllerDisconnected(player_index); return true; } diff --git a/src/frontend-common/sdl_controller_interface.h b/src/frontend-common/sdl_controller_interface.h index f3ba1a45a..345f278ea 100644 --- a/src/frontend-common/sdl_controller_interface.h +++ b/src/frontend-common/sdl_controller_interface.h @@ -52,8 +52,7 @@ private: ControllerDataVector::iterator GetControllerDataForPlayerId(int id); bool OpenGameController(int index); - bool CloseGameController(int joystick_index); - void CloseGameControllers(); + bool CloseGameController(int joystick_index, bool notify); bool HandleControllerAxisEvent(const SDL_Event* event); bool HandleControllerButtonEvent(const SDL_Event* event);