From ce11a1dfa91a184d4c641f0060dbaafceaa77ee5 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 28 Jan 2018 21:19:03 +0200 Subject: [PATCH] [Client] Fix potential invalid pointer when storing cell unloads Notably, builds of the client done with Clang were crashing on startup before this. --- apps/openmw/mwmp/LocalPlayer.cpp | 2 +- apps/openmw/mwmp/LocalPlayer.hpp | 2 +- apps/openmw/mwworld/scene.cpp | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index 5973ffea3..56a426807 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -1544,7 +1544,7 @@ void LocalPlayer::clearCurrentContainer() currentContainer.mpNum = 0; } -void LocalPlayer::storeCellState(ESM::Cell cell, int stateType) +void LocalPlayer::storeCellState(const ESM::Cell& cell, int stateType) { std::vector::iterator iter; diff --git a/apps/openmw/mwmp/LocalPlayer.hpp b/apps/openmw/mwmp/LocalPlayer.hpp index 6f1dc9e07..1900b07b4 100644 --- a/apps/openmw/mwmp/LocalPlayer.hpp +++ b/apps/openmw/mwmp/LocalPlayer.hpp @@ -91,7 +91,7 @@ namespace mwmp void clearCellStates(); void clearCurrentContainer(); - void storeCellState(ESM::Cell cell, int stateType); + void storeCellState(const ESM::Cell& cell, int stateType); void storeCurrentContainer(const MWWorld::Ptr& container); void playAnimation(); diff --git a/apps/openmw/mwworld/scene.cpp b/apps/openmw/mwworld/scene.cpp index 35161dca2..58e6200a7 100644 --- a/apps/openmw/mwworld/scene.cpp +++ b/apps/openmw/mwworld/scene.cpp @@ -243,6 +243,17 @@ namespace MWWorld std::cout << "Unloading cell\n"; ListAndResetObjectsVisitor visitor; + /* + Start of tes3mp addition + + Set a const pointer to the iterator's ESM::Cell here, because + (*iter)->getCell() can become invalid later down + */ + const ESM::Cell* cell = (*iter)->getCell(); + /* + End of tes3mp addition + */ + (*iter)->forEach(visitor); for (std::vector::const_iterator iter2 (visitor.mObjects.begin()); iter2!=visitor.mObjects.end(); ++iter2) @@ -276,7 +287,7 @@ namespace MWWorld Store a cell unload for the LocalPlayer */ - mwmp::Main::get().getLocalPlayer()->storeCellState(*(*iter)->getCell(), mwmp::CellState::UNLOAD); + mwmp::Main::get().getLocalPlayer()->storeCellState(*cell, mwmp::CellState::UNLOAD); /* End of tes3mp addition */