Fix string leaking (and destructors not getting called) when there are XMLNodes that aren't in the document tree
diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index 152e3d2..9855dcf 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -802,6 +802,8 @@
child->_next->_prev = child->_prev;
}
child->_parent = 0;
+ child->_next = 0;
+ child->_prev = 0;
}
@@ -811,6 +813,9 @@
TIXMLASSERT( node->_document == _document );
TIXMLASSERT( node->_parent == this );
Unlink( node );
+ TIXMLASSERT(node->_prev == 0);
+ TIXMLASSERT(node->_next == 0);
+ TIXMLASSERT(node->_parent == 0);
DeleteNode( node );
}
@@ -1055,11 +1060,16 @@
return 0;
}
-void XMLNode::DeleteNode( XMLNode* node )
+/*static*/ void XMLNode::DeleteNode( XMLNode* node )
{
if ( node == 0 ) {
return;
}
+ TIXMLASSERT(node->_document);
+ if (!node->ToDocument()) {
+ node->_document->MarkInUse(node);
+ }
+
MemPool* pool = node->_memPool;
node->~XMLNode();
pool->Free( node );
@@ -1070,10 +1080,13 @@
TIXMLASSERT( insertThis );
TIXMLASSERT( insertThis->_document == _document );
- if ( insertThis->_parent )
+ if (insertThis->_parent) {
insertThis->_parent->Unlink( insertThis );
- else
+ }
+ else {
+ insertThis->_document->MarkInUse(insertThis);
insertThis->_memPool->SetTracked();
+ }
}
const XMLElement* XMLNode::ToElementWithName( const char* name ) const
@@ -1979,9 +1992,24 @@
}
+void XMLDocument::MarkInUse(XMLNode* node)
+{
+ TIXMLASSERT(node->_parent == 0);
+
+ for (int i = 0; i < _unlinked.Size(); ++i) {
+ if (node == _unlinked[i]) {
+ _unlinked.SwapRemove(i);
+ break;
+ }
+ }
+}
+
void XMLDocument::Clear()
{
DeleteChildren();
+ while( _unlinked.Size()) {
+ DeleteNode(_unlinked[0]); // Will remove from _unlinked as part of delete.
+ }
#ifdef DEBUG
const bool hadError = Error();