From 7b26058fa51fc1db24d8583b094d20add4b1a0a1 Mon Sep 17 00:00:00 2001 From: Frederic Chardon Date: Mon, 6 Sep 2021 11:47:21 +0200 Subject: [PATCH] moveObject() has side effects that might invalidate iterators from mActors. Instead of iterating over mActors, make a copy of needed data and iterate over the copies. --- apps/openmw/mwphysics/physicssystem.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/openmw/mwphysics/physicssystem.cpp b/apps/openmw/mwphysics/physicssystem.cpp index d248b599ee..b3c4ef6168 100644 --- a/apps/openmw/mwphysics/physicssystem.cpp +++ b/apps/openmw/mwphysics/physicssystem.cpp @@ -813,12 +813,20 @@ namespace MWPhysics { auto* player = getActor(MWMechanics::getPlayer()); auto* world = MWBase::Environment::get().getWorld(); - for (auto& [ptr, physicActor] : mActors) + + // copy new ptr position in temporary vector. player is handled separately as its movement might change active cell. + std::vector> newPositions; + newPositions.reserve(mActors.size() - 1); + for (const auto& [ptr, physicActor] : mActors) { if (physicActor.get() == player) continue; - world->moveObject(physicActor->getPtr(), physicActor->getSimulationPosition(), false, false); + newPositions.emplace_back(physicActor->getPtr(), physicActor->getSimulationPosition()); } + + for (auto& [ptr, pos] : newPositions) + world->moveObject(ptr, pos, false, false); + world->moveObject(player->getPtr(), player->getSimulationPosition(), false, false); }