Hi,
I just discovered a memory leak that happens if you do not call LinkEndChild().
So if you code like so:
TiXmlElement * pRoot = new TiXmlElement(“BOOKS”);
xmlDoc.LinkEndChild(pRoot);
there is no memory leak.
But if the code goes like this:
TiXmlElement * pRoot = new TiXmlElement(“BOOKS”); <- memory leak here
And I have to call
delete pRoot;
Just to explain what I am doing.
I want to create <BOOKS> element, add number of <BOOK> elements to it, all in memory.
Then I load the document, check if it contains <BOOKS> element. If it does then I want to replace it with the one I just created, if it does not I want to insert it.
Am I missing something or am I doing the right thing calling “delete”?
I do not believe I’ve seen a “delete” statement anywhere in the docs, so I am a bit confused.
Thanks,
Igor.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Igor, if it's not clear from the comment above, TinyXML will delete objects you pass to it. (LinkEndChild()) but you need to call 'delete' on objects you do not pass to TinyXML.
lee
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I just discovered a memory leak that happens if you do not call LinkEndChild().
So if you code like so:
TiXmlElement * pRoot = new TiXmlElement(“BOOKS”);
xmlDoc.LinkEndChild(pRoot);
there is no memory leak.
But if the code goes like this:
TiXmlElement * pRoot = new TiXmlElement(“BOOKS”); <- memory leak here
And I have to call
delete pRoot;
Just to explain what I am doing.
I want to create <BOOKS> element, add number of <BOOK> elements to it, all in memory.
Then I load the document, check if it contains <BOOKS> element. If it does then I want to replace it with the one I just created, if it does not I want to insert it.
Am I missing something or am I doing the right thing calling “delete”?
I do not believe I’ve seen a “delete” statement anywhere in the docs, so I am a bit confused.
Thanks,
Igor.
do you mean:
// this is ok - no memory leak
{
TiXmlDoc doc...
TiXmlElement * pRoot = new TiXmlElement("BOOKS");
doc.LinkEndChild(pRoot);
}
// this is bad - memory leak
{
TiXmlElement * pRoot = new TiXmlElement("BOOKS");
}
?
Hi. This means tha ti can create several TiXmlElement like this and have no leak?
pXmlElFile = new TiXmlElement( "file" );
pXmlElFile->SetAttribute("name",str);
pXmlElFile->SetAttribute("id",unicNumber);
//Username
TiXmlElement* xmlEl = new TiXmlElement("username");
TiXmlText* xmlText = new TiXmlText(userName);
xmlEl->LinkEndChild(xmlText);
pXmlElFile->LinkEndChild(xmlEl);
//url
xmlEl = new TiXmlElement("url");
xmlText = new TiXmlText(url);
xmlEl->LinkEndChild(xmlText);
pXmlElFile->LinkEndChild(xmlEl);
//password
xmlEl = new TiXmlElement("password");
xmlText = new TiXmlText(passwordEnc);
xmlEl->LinkEndChild(xmlText);
pXmlElFile->LinkEndChild(xmlEl);
....
....
....
....
Yes.
Igor, if it's not clear from the comment above, TinyXML will delete objects you pass to it. (LinkEndChild()) but you need to call 'delete' on objects you do not pass to TinyXML.
lee
Thanks a lot, Lee. I just wanted to make sure that I am doing the right thing calling 'delete' since the documentation does not mention this anywhere.
Thanks again.