From 07e5ade69918341666511a8b33aef04479705262 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Tue, 19 Dec 2017 13:11:40 +0100 Subject: 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. --- src/game/game.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src/game/game.cpp') 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_) -- cgit