From af7ef732bd497dd664b3cc97f9f1723545627593 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sat, 15 Oct 2011 18:06:24 +0000 Subject: [PATCH] Harden unit_movement_resetter class. Add a warning regarding its usage and test whether the unit exists before assigning to it. (Still a bit experimental, but initial tests show no regressions.) --- src/unit.cpp | 8 ++++++++ src/unit.hpp | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/unit.cpp b/src/unit.cpp index 73ebfaab401..af07a157299 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -2799,6 +2799,14 @@ unit_movement_resetter::unit_movement_resetter(unit &u, bool operate) : unit_movement_resetter::~unit_movement_resetter() { + assert(resources::units); + + /* + * This assert should be safe, but not 100% sure so if it fails need + * some extra validation code in this function. + */ + assert(resources::units->has_unit(&u_)); + u_.movement_ = moves_; } diff --git a/src/unit.hpp b/src/unit.hpp index c295399a2ec..81de17f2cc8 100644 --- a/src/unit.hpp +++ b/src/unit.hpp @@ -467,8 +467,16 @@ private: void clear_visibility_cache() const { invisibility_cache_.clear(); } }; -/** Object which temporarily resets a unit's movement */ +/** + * Object which temporarily resets a unit's movement. + * + * @warning + * The unit whose movement is reset may not be deleted while a + * @ref unit_movement_resetter object 'holds'. So best use it only in a small + * scope. + */ struct unit_movement_resetter + : private boost::noncopyable { unit_movement_resetter(unit& u, bool operate=true); ~unit_movement_resetter();