otland / forgottenserver

A free and open-source MMORPG server emulator written in C++
https://otland.net
GNU General Public License v2.0
1.56k stars 1.04k forks source link

Fix possible crash #4673

Closed kygov closed 2 months ago

kygov commented 2 months ago

Pull Request Prelude

Changes Proposed

Fixed broken iteration.

Issues addressed: Crash because removing element from map and iterating.

kygov commented 2 months ago

This doesn't work either as expected, I'd suggest to do it the safe way with 2 loops

std::vector<std::string> invalid;
for (auto& event : creatureEvents) {
  if (event.second.getScriptId() == 0) {
      invalid.push_back(event.first);
  }
}
for (auto& toRemove : invalid) {
  creatureEvents.erase(toRemove);
}
invalid.clear();

What's not working as expected?

EvilHero90 commented 2 months ago

What's not working as expected?

tried it on different online debuggers, the first one I tried gave weird outcomes, others work just fine sorry my mistake

nekiro commented 2 months ago

could be std::erase_if