From 64573001391fe9e27b70cc059ab5b32f6041de99 Mon Sep 17 00:00:00 2001 From: starry wisdom Date: Wed, 19 Aug 2020 16:52:48 +0100 Subject: [PATCH] Fix crash on restarting a scenario due to invalid iterators --- src/scienceDatabase.cpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/scienceDatabase.cpp b/src/scienceDatabase.cpp index b3ca088585..16f6269b39 100644 --- a/src/scienceDatabase.cpp +++ b/src/scienceDatabase.cpp @@ -55,20 +55,18 @@ void ScienceDatabase::destroy() if(this->isDestroyed()) return; auto my_id = this->getId(); - ScienceDatabase::science_databases.remove(this); MultiplayerObject::destroy(); + ScienceDatabase::science_databases.remove(this); - PVector::iterator it = ScienceDatabase::science_databases.begin(); - - while(it != ScienceDatabase::science_databases.end()) { - if (!(*it)->isDestroyed() && (*it)->getParentId() == my_id) - { - (*it)->destroy(); - } - else + while (true) + { + // we don't save a iterator between each loop as when we call ScienceDatabase::destroy our iterator may be invalidated + auto it=find_if(ScienceDatabase::science_databases.begin(),ScienceDatabase::science_databases.end(),[my_id](P db){return !db->isDestroyed() && db->getParentId() == my_id;}); + if (it == ScienceDatabase::science_databases.end()) { - ++it; + break; } + (*it)->destroy(); } } @@ -269,19 +267,16 @@ static string directionLabel(float direction) void flushDatabaseData() { - PVector::iterator it = ScienceDatabase::science_databases.begin(); - - while(it != ScienceDatabase::science_databases.end()) { - if (!(*it)->isDestroyed()) + while(!ScienceDatabase::science_databases.empty()) { + if (!ScienceDatabase::science_databases.back()->isDestroyed()) { - (*it)->destroy(); + ScienceDatabase::science_databases.back()->destroy(); } else { - ++it; + ScienceDatabase::science_databases.pop_back(); } } - ScienceDatabase::science_databases.resize(0); } void fillDefaultDatabaseData()