diff --git a/code/DDLNode.cpp b/code/DDLNode.cpp index ce10c1c..3955e3f 100644 --- a/code/DDLNode.cpp +++ b/code/DDLNode.cpp @@ -77,8 +77,8 @@ DDLNode::~DDLNode() { if( s_allocatedNodes[ m_idx ] == this ) { s_allocatedNodes[ m_idx ] = ddl_nullptr; } - for(size_t i = 0 ; i::iterator it; - it = std::find( m_parent->m_children.begin(), m_parent->m_children.end(), this ); + if( ddl_nullptr != m_parent ) { + DDLNodeIt it = std::find( m_parent->m_children.begin(), m_parent->m_children.end(), this ); if( m_parent->m_children.end() != it ) { m_parent->m_children.erase( it ); } @@ -202,7 +201,7 @@ DDLNode *DDLNode::create( const std::string &type, const std::string &name, DDLN void DDLNode::releaseNodes() { if( s_allocatedNodes.size() > 0 ) { - for( DllNodeList::iterator it = s_allocatedNodes.begin(); it != s_allocatedNodes.end(); it++ ) { + for( DDLNodeIt it = s_allocatedNodes.begin(); it != s_allocatedNodes.end(); it++ ) { if( *it ) { delete *it; } diff --git a/include/openddlparser/DDLNode.h b/include/openddlparser/DDLNode.h index 1371356..184cd01 100644 --- a/include/openddlparser/DDLNode.h +++ b/include/openddlparser/DDLNode.h @@ -53,6 +53,9 @@ class DLL_ODDLPARSER_EXPORT DDLNode { /// @brief The child-node-list type. typedef std::vector DllNodeList; + /// @brief The child-node-list iterator. + typedef std::vector::iterator DDLNodeIt; + public: /// @brief The class destructor. ~DDLNode(); diff --git a/test/DDLNodeTest.cpp b/test/DDLNodeTest.cpp index 9a0cd42..2e1f2ac 100644 --- a/test/DDLNodeTest.cpp +++ b/test/DDLNodeTest.cpp @@ -46,6 +46,22 @@ TEST_F( DDLNodeTest, createDDLNodeTest ) { delete myNode; } +TEST_F( DDLNodeTest, attach_detach_parent_Test ) { + DDLNode *myParent = DDLNode::create( "parent", "" ); + DDLNode *myChild = DDLNode::create( "child", "" ); + EXPECT_EQ( nullptr, myParent->getParent() ); + myChild->attachParent( myParent ); + EXPECT_EQ( myParent, myChild->getParent() ); + + myChild->detachParent(); + EXPECT_EQ( nullptr, myChild->getParent() ); + + // must not fail + myChild->detachParent(); + myParent->detachParent(); + myParent->attachParent( nullptr ); +} + TEST_F( DDLNodeTest, accessTypeTest ) { static const std::string type1 = "type"; static const std::string name1 = "test";