Skip to content

Commit

Permalink
fix: crash when spawn cleanup (#3034)
Browse files Browse the repository at this point in the history
Crash fix introduced in #2913 

This crash occurs when the iterator attempts to access a container that
was modified during the creature removal process, while
spawnedMonsterMap and/or erase_if is being executed.
  • Loading branch information
kaleohanopahala authored Oct 31, 2024
1 parent 00b7509 commit cba2abe
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,18 @@ void SpawnMonster::scheduleSpawn(uint32_t spawnMonsterId, spawnBlock_t &sb, cons
}

void SpawnMonster::cleanup() {
std::erase_if(spawnedMonsterMap, [this](const auto &entry) {
const auto &monster = entry.second;
for (auto it = spawnedMonsterMap.begin(); it != spawnedMonsterMap.end(); ) {
const auto &monster = it->second;
if (!monster || monster->isRemoved()) {
auto spawnIt = spawnMonsterMap.find(entry.first);
auto spawnIt = spawnMonsterMap.find(it->first);
if (spawnIt != spawnMonsterMap.end()) {
spawnIt->second.lastSpawn = OTSYS_TIME();
}
return true;
it = spawnedMonsterMap.erase(it);
} else {
++it;
}
return false;
});
}
}

const Position &SpawnMonster::getCenterPos() const {
Expand Down

0 comments on commit cba2abe

Please sign in to comment.