Merge branch 'fix_delete_game_ub' into 'master'
Some checks are pending
Build and test / Windows (2019) (push) Blocked by required conditions
Build and test / Windows (2022) (push) Blocked by required conditions
Build and test / Ubuntu (push) Waiting to run
Build and test / MacOS (push) Waiting to run
Build and test / Read .env file and expose it as output (push) Waiting to run

Avoid accessing removed character on deleting last save (#8387)

Closes #8387

See merge request OpenMW/openmw!4574
This commit is contained in:
Alexei Kotov 2025-03-09 10:42:11 +03:00
commit 01ea2ad08c
3 changed files with 5 additions and 4 deletions

View file

@ -38,7 +38,7 @@ MWState::Character* MWState::CharacterManager::getCurrentCharacter()
return mCurrent;
}
void MWState::CharacterManager::deleteSlot(const MWState::Character* character, const MWState::Slot* slot)
void MWState::CharacterManager::deleteSlot(const MWState::Slot* slot, const MWState::Character*& character)
{
std::list<Character>::iterator it = findCharacter(character);
@ -51,6 +51,7 @@ void MWState::CharacterManager::deleteSlot(const MWState::Character* character,
if (character == mCurrent)
mCurrent = nullptr;
mCharacters.erase(it);
character = nullptr;
}
}

View file

@ -33,7 +33,7 @@ namespace MWState
Character* getCurrentCharacter();
///< @note May return null
void deleteSlot(const MWState::Character* character, const MWState::Slot* slot);
void deleteSlot(const MWState::Slot* slot, const Character*& character);
Character* createCharacter(const std::string& name);
///< Create new character within saved game management

View file

@ -706,10 +706,10 @@ void MWState::StateManager::quickLoad()
void MWState::StateManager::deleteGame(const MWState::Character* character, const MWState::Slot* slot)
{
const std::filesystem::path savePath = slot->mPath;
mCharacterManager.deleteSlot(character, slot);
mCharacterManager.deleteSlot(slot, character);
if (mLastSavegame == savePath)
{
if (character->begin() != character->end())
if (character != nullptr)
mLastSavegame = character->begin()->mPath;
else
mLastSavegame.clear();