Re: [xml] xmlAddChild question



My regrets.  Yes, I meant xmlAddChild.  For clarity, the original code was like this (error processing has 
been removed):

          xmlNodePtr   pTextNode, pChildNode;

        pTextNode = xmlNewText(pUtf8Value);  // pUtr8Value is an xmlCharPtr parameter

        if ( pTextNode != NULL )  {
            pChildNode = xmlAddChild(pParentNode, pTextNode);  // pParentNode is an xmlNodePtr parameter
            if ( pChildNode == NULL )
                xmlFreeNode(pTextNode);
                  // ... error processing
          }
        else // ... error processing

To eliminate the leak, I made all of the above conditional on pUtf8Value[0] != '\0'.  The leak seemed to be 
that pTextNode was neither freed nor linked when it had empty content.  I know that the document was freed 
after each document was created in the loop of document creations (otherwise, the leak would have been much 
larger because the document was quite large).  

My original question was trying to clarify to me the "merging" condition on whether cur was freed or not 
freed.  My current understanding is that if the return value of xmlAddChild differs from cur, then cur has 
been merged and freed except when the return value is NULL;  if the return value is the same as cur, then it 
has been linked and will be freed with the parent such as when the document is freed.  Thus, cur never needs 
to be freed by the caller except in error situations where xmlAddChild returns NULL.  Unfortunately, that 
does not explain the memory leak I was seeing.


Bruce Sinclair
Software Systems Developer - Senior Principal

Micro Focus

bruce sinclair microfocus com
8310 N. Capital of Texas Hwy.
Building Two, Suite 100
Austin, Texas  78731-1002
Direct: +1 512 340 4717


-----Original Message-----
From: Daniel Veillard [mailto:veillard redhat com] 
Sent: 2012 April 16 Monday 9:04 AM
To: Bruce Sinclair
Cc: xml gnome org
Subject: Re: [xml] xmlAddChild question

On Mon, Apr 16, 2012 at 08:52:06AM -0500, Bruce Sinclair wrote:
Daniel,

Thanks, but I think you omitted a special case.  My memory leak was 
caused when I used xmlChildNode to add an empty text node.
The xmlChildNode function succeeded (non-null returned) but the text

  You mean xmlAddChild() right ? that's what we talked about and there is no xmlChildNode() function ?!?

node was neither freed nor linked into the tree.  This lost 109 bytes 
per occurrence and was inside a large document with many empty text 
nodes being constructed multiple times (the document was freed before 
constructing another).  Eventually, the leak overwhelmed the memory 
manager.  I fixed the leak by testing for an empty text node and not 
attempting to add it as a child.  I was just curious if this was a bug 
or my lack of understanding of xmlChildNode.  You explanation helps, 
but did not address the addition of an empty text node.

  That sounds like a bug, but I really don't see how that could happen.
Actually none of the code of xmlAddChild check for a null content of a text none. Either it appends 
cur->content to existing content and free cur unconditionally or it adds the children to the list.
  Are you sure it's the right function ?

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel veillard com  | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization 
library  http://libvirt.org/

This message has been scanned by MailController - portal1.mailcontroller.co.uk



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]