Menu

memory leak in Clone

Developer
1234
2007-08-07
2013-05-20
  • 1234

    1234 - 2007-08-07

    I am getting memory leak in TinyXml if I clone a node.
    Is this going to be fixed?

    Thanks in Advance!

     
    • Ellers

      Ellers - 2007-08-08

      Can you be more specific?
      Example code that clearly reproduces the leak, for example.
      Ellers

       
    • 1234

      1234 - 2007-08-10

      I am using BoundsChecker and if I load a file and make clone of any child it is showing leak in Clone. Do I need to destroy the cloned node myself if yes then how?

       
      • Ellers

        Ellers - 2007-08-10

        Again, example code would make this easier to diagnose.
        In the absence of that, I'll take a guess...

        If you do
          {
            thing = new TiXmlNode();
          }

        You have a leak.

        But this will not have a leak:
          {
            thing = new TiXmlNode();
            delete thing;
          }

        Similarly, this:
          {
            thing = src->Clone();
          }

        is clearly a leak, but the following is not:

          {
            thing = src->Clone();
            delete thing;
          }

        Recall the doc from TinyXml.h:

          /** Create an exact duplicate of this node and return it. The memory must be deleted
              by the caller.
           */
           virtual TiXmlNode* Clone() const = 0;

        alternatively, if you make the node owned by something else that gets cleaned up (either by explicit deleting or by implict deleting when the stack is unwound) then you'll have no leak:

          {
            TiXmlDocument doc;
            ...
            TiXmlElement copy = someNode->Clone()
            doc->LinkEndChild(copy); // node is now "owned"
           
          } // doc will be deleted as stack unwinds, and as doc owns copy, then copy will be deleted

        HTH
        Ellers

         

Log in to post a comment.