Supose for an ill programmed routine that a node A is attached to node B and that the node B is attached to node C, currently, it is possible to attach node A again to node C because the only check the scene nodes do is that the parent node of a given node isn't the one that is going to be inserted, how is prevented that A is attached to C? This could create a loop within the tree, and force a stack overflow because of the traversing routine, and end the program.
Shouldn't all the ancestors be checked first? eventhough it is a linear check, it is unlikely it lasts long so it could be worth, something like this:
Code: Select all
...
//Preventing cycles in the tree, node is the node to be inserted, which we already checked is not null
ISceneNode* ancestor = this; //We start by ourselves...
while (ancestor != nullptr) //the root of the tree has no more ancestors, thus is safe to insert
if (ancestor == node)
return; //The node we want to insert is already one of our ancestors, prevent the insertion!
else
ancestor = ancestor->parent; //move up the hierarchy
...
//do something else, like inserting the node as a child of ours...