Menu

Memory leak if LinkEndChild() is not called

igorv007
2005-12-18
2013-05-20
  • igorv007

    igorv007 - 2005-12-18

    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.

     
    • Ellers

      Ellers - 2005-12-18

      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");
      }

      ?

       
      • David Jorge

        David Jorge - 2006-03-29

        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);

        ....
        ....
        ....
        ....

         
    • igorv007

      igorv007 - 2005-12-18

      Yes.

       
    • Lee Thomason

      Lee Thomason - 2005-12-19

      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

       
    • igorv007

      igorv007 - 2005-12-19

      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.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.