summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-12-19 13:11:40 +0100
committerPaweł Redman <pawel.redman@gmail.com>2017-12-19 13:11:40 +0100
commit07e5ade69918341666511a8b33aef04479705262 (patch)
tree5a5bbf457ea5f56a87bfe7c793dd39a6cde8fa47
parent48f82598b147358316d6bcf9803df1d14de35143 (diff)
Fix a segfault in the wake/sleep mechanism.
The problem was that on_think can invalidate *any* iterator in the map (e.g. a spider killing a human), not just the current one.
-rw-r--r--src/game/game.cpp12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 9d184ea..fedc403 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -146,13 +146,11 @@ void state_t::tick(double now_, double dt_)
u.d = dt;
dice_prng.seed(dice_prng.next() ^ u.i);
- // NOTE: on_think can erase the entity from awake_entities.
- for (auto i = awake_entities.begin(); i != awake_entities.end(); ) {
- auto next = i;
- next++;
- (*i)->on_think();
- i = next;
- }
+ // on_think can insert/erase elements of awake_entities so iterate
+ // over a copy of it.
+ auto copy = awake_entities;
+ for (entity_t *ent : copy)
+ ent->on_think();
}
die_t::die_t(size_t sides_)