@@ -496,28 +496,51 @@ void GameStateLoad::logic() {
496496confirm->logic ();
497497if (confirm->clicked_confirm ) {
498498if (confirm->action_list ->getSelected () == DELETE_CONFIRM_OPTION_YES) {
499- Utils::removeSaveDir (game_slots[selected_slot]->id );
499+ if (selected_slot != -1 &&static_cast <size_t >(selected_slot) < game_slots.size ()) {
500+ Utils::removeSaveDir (game_slots[selected_slot]->id );
501+
502+ delete game_slots[selected_slot];
503+ game_slots[selected_slot] =NULL ;
504+ game_slots.erase (game_slots.begin ()+selected_slot);
505+
506+ visible_slots = (game_slot_max >static_cast <int >(game_slots.size ()) ?static_cast <int >(game_slots.size ()) : game_slot_max);
507+ if (!game_slots.empty ()) {
508+ if (selected_slot >0 )
509+ setSelectedSlot (selected_slot -1 );
510+ else
511+ setSelectedSlot (0 );
512+ }
513+ else {
514+ setSelectedSlot (-1 );
515+ }
500516
501- delete game_slots[selected_slot];
502- game_slots[selected_slot] =NULL ;
503- game_slots.erase (game_slots.begin ()+selected_slot);
517+ while (scroll_offset + visible_slots >static_cast <int >(game_slots.size ())) {
518+ scroll_offset--;
519+ }
520+
521+ scrollToSelected ();
522+ updateButtons ();
523+
524+ refreshSavePaths ();
525+ settings->prev_save_slot = -1 ;
526+
527+ }
528+ tablist.defocus ();
529+ }
530+ else {
531+ // We shouldn't end up here!
532+ // But if we do, log an error and try to reset the UI
533+
534+ Utils::logError (" GameStateLoad: Can't delete save. Index at %d is out-of-bounds." , selected_slot);
504535
505- visible_slots = (game_slot_max >static_cast <int >(game_slots.size ()) ?static_cast <int >(game_slots.size ()) : game_slot_max);
506536if (!game_slots.empty ())
507537setSelectedSlot (0 );
508538else
509539setSelectedSlot (-1 );
510540
511- while (scroll_offset + visible_slots >static_cast <int >(game_slots.size ())) {
512- scroll_offset--;
513- }
514-
515541scrollToSelected ();
516542updateButtons ();
517-
518- refreshSavePaths ();
519543settings->prev_save_slot = -1 ;
520-
521544tablist.defocus ();
522545}
523546