Skip to content

Commit

Permalink
Fix crash on restarting a scenario due to invalid iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
StarryWisdom authored and daid committed Aug 24, 2020
1 parent bbbc845 commit 6457300
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/scienceDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScienceDatabase>::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<ScienceDatabase> db){return !db->isDestroyed() && db->getParentId() == my_id;});
if (it == ScienceDatabase::science_databases.end())
{
++it;
break;
}
(*it)->destroy();
}
}

Expand Down Expand Up @@ -269,19 +267,16 @@ static string directionLabel(float direction)

void flushDatabaseData()
{
PVector<ScienceDatabase>::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()
Expand Down

0 comments on commit 6457300

Please sign in to comment.