From 3150aa2eadb47f75447d9a8318a612a67e920427 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Fri, 15 Aug 2014 11:03:21 +0200 Subject: [PATCH] Removed assert(active_item < int(items.size())); as that is no longer save to call due to Menu being potentially deleted at that point --- src/gui/menu.cpp | 23 +++++++++++------------ src/gui/menu.hpp | 6 +----- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index b45ae0623..ec3c9dbbb 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -37,7 +37,6 @@ static const float MENU_REPEAT_RATE = 0.1f; Menu::Menu() : pos(), - menuaction(), delete_character(), mn_input_char(), menu_repeat_time(), @@ -45,7 +44,6 @@ Menu::Menu() : arrange_left(), active_item() { - menuaction = MENU_ACTION_NONE; delete_character = 0; mn_input_char = '\0'; @@ -170,7 +168,6 @@ Menu::clear() active_item = -1; } -/* Process actions done on the menu */ void Menu::process_input() { @@ -181,6 +178,7 @@ Menu::process_input() pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f; } + MenuAction menuaction = MENU_ACTION_NONE; Controller* controller = g_input_manager->get_controller(); /** check main input controller... */ if(controller->pressed(Controller::UP)) { @@ -235,6 +233,15 @@ Menu::process_input() if(items.size() == 0) return; + // The menu_action() call can pop() the menu from the stack and thus + // delete it, so it's important that no further member variables are + // accessed after this call + process_action(menuaction); +} + +void +Menu::process_action(MenuAction menuaction) +{ int last_active_item = active_item; switch(menuaction) { case MENU_ACTION_UP: @@ -363,16 +370,9 @@ Menu::process_input() case MENU_ACTION_NONE: break; } - menuaction = MENU_ACTION_NONE; - - assert(active_item < int(items.size())); } void -Menu::menu_action(MenuItem* ) -{} - -void Menu::draw_item(DrawingContext& context, int index) { float menu_height = get_height(); @@ -656,7 +656,6 @@ Menu::set_toggled(int id, bool toggled) get_item_by_id(id).toggled = toggled; } -/* Check for menu event */ void Menu::event(const SDL_Event& event) { @@ -673,7 +672,7 @@ Menu::event(const SDL_Event& event) y > pos.y - get_height()/2 && y < pos.y + get_height()/2) { - menuaction = MENU_ACTION_HIT; + process_action(MENU_ACTION_HIT); } } break; diff --git a/src/gui/menu.hpp b/src/gui/menu.hpp index 11bd45aec..5ecd3f877 100644 --- a/src/gui/menu.hpp +++ b/src/gui/menu.hpp @@ -28,8 +28,6 @@ class DrawingContext; class MenuItem; -bool confirm_dialog(Surface* background, std::string text); - class Menu { static Color default_color; @@ -106,6 +104,7 @@ protected: MenuItem* add_item(std::unique_ptr menu_item); private: + void process_action(MenuAction menuaction); void check_controlfield_change_event(const SDL_Event& event); void draw_item(DrawingContext& context, int index); @@ -113,9 +112,6 @@ private: // position of the menu (ie. center of the menu, not top/left) Vector pos; - /** input event for the menu (up, down, left, right, etc.) */ - MenuAction menuaction; - /* input implementation variables */ int delete_character; char mn_input_char; -- 2.11.0