diff --git a/include/ISceneNode.h b/include/ISceneNode.h index 7e02628e..304888f9 100644 --- a/include/ISceneNode.h +++ b/include/ISceneNode.h @@ -277,8 +277,9 @@ namespace scene child->grab(); child->remove(); // remove from old parent Children.push_back(child); - child->Iterator = Children.end(); - (*child->Iterator)--; + // Note: This iterator is not invalidated until we erase it. + child->ThisIterator = Children.end(); + --(*child->ThisIterator); child->Parent = this; } } @@ -294,8 +295,9 @@ namespace scene if (child->Parent != this) return false; - auto it = child->Iterator.value(); - child->Iterator = std::nullopt; + // The iterator must be set since the parent is not null. + auto it = *child->ThisIterator; + child->ThisIterator = std::nullopt; child->Parent = nullptr; child->drop(); Children.erase(it); @@ -311,7 +313,7 @@ namespace scene { for (auto &child : Children) { child->Parent = nullptr; - child->Iterator = std::nullopt; + child->ThisIterator = std::nullopt; child->drop(); } Children.clear(); @@ -617,11 +619,12 @@ namespace scene //! Pointer to the parent ISceneNode* Parent; - std::optional Iterator; - //! List of all children of this node std::list Children; + //! Iterator pointing to this node in the parent's child list. + std::optional ThisIterator; + //! Pointer to the scene manager ISceneManager* SceneManager;