Correct way to clean up.

37 views
Skip to first unread message

Michael Williamson

unread,
Dec 26, 2018, 1:55:53 PM12/26/18
to TinyXML++
Hello,

I am using TinyXML++ to build XML content up at runtime initialization, so I have code that looks like:

TiXmlDocument* pdoc = new TiXmlDocument();
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "UTF-8", "");
XmlDoc->LinkEndChild(decl);

TiXmlElement* root = new TiXmlElement("root");
...
while ( more_els_to_add ) {
TiXmlElement* el = new TiXmlElement(...);
...
root->LinkEndChild(el);
}
XmlDoc->LinkEndChild( root );

TiXmlPrinter printer;
pdoc->Accept(&printer);
std::string XmlData = printer.CStr();
delete XmlDoc;

At the end of initialization, all I really need is the XmlData string and I'd like to release the TinyXML DOM data once I have it.

My question is, can I just delete pdoc, and will that clean up all of the children linked to it? It looks like there is some reference counting going on in the code but I wasn't sure exactly what the intent was. It looks like after to the LinkEndChild( foo ) calls I need to delete the local pointer reference or use Non-pointer references and let them go out of scope naturally, e.g.:

while ( more_els_to_add ) {
TiXmlElement* el = new TiXmlElement(...);
...
root->LinkEndChild(el);
delete el; // OK we are reference counting under the hood ?
}

or

while ( more_els_to_add ) {
TiXmlElement el = TiXmlElement(...);
...
root->LinkEndChild(&el);
// el goes out of scope, but this is OK as we are reference counting?
}

Is there any guidance on this? Did I miss it in the doc area?

Thanks!

-Mike

Reply all
Reply to author
Forward
0 new messages