Skip to content

Commit

Permalink
SpawnManager: Fix self-invalidating iterator in Update loop
Browse files Browse the repository at this point in the history
  • Loading branch information
insunaa committed Nov 29, 2023
1 parent 517473c commit 82db66b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/game/Maps/SpawnManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ void SpawnManager::Initialize()
void SpawnManager::AddCreature(uint32 dbguid)
{
time_t respawnTime = m_map.GetPersistentState()->GetCreatureRespawnTime(dbguid);
m_spawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_UNIT);
std::sort(m_spawns.begin(), m_spawns.end());
m_spawnInserts.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_UNIT);
}

void SpawnManager::AddGameObject(uint32 dbguid)
{
time_t respawnTime = m_map.GetPersistentState()->GetGORespawnTime(dbguid);
m_spawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_GAMEOBJECT);
std::sort(m_spawns.begin(), m_spawns.end());
m_spawnInserts.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_GAMEOBJECT);
}

void SpawnManager::RespawnCreature(uint32 dbguid, uint32 respawnDelay)
Expand Down Expand Up @@ -160,6 +158,9 @@ void SpawnManager::RespawnAll()

void SpawnManager::Update()
{
std::move(m_spawnInserts.begin(), m_spawnInserts.end(), std::back_inserter(m_spawns));
m_spawnInserts.clear();
std::sort(m_spawns.begin(), m_spawns.end());
auto now = m_map.GetCurrentClockTime();
for (auto itr = m_spawns.begin(); itr != m_spawns.end();)
{
Expand Down
1 change: 1 addition & 0 deletions src/game/Maps/SpawnManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class SpawnManager
Map& m_map;

std::vector<SpawnInfo> m_spawns; // must only be erased from in Update
std::vector<SpawnInfo> m_spawnInserts;
std::map<uint32, SpawnGroup*> m_spawnGroups;
};

Expand Down

0 comments on commit 82db66b

Please sign in to comment.