Re: [xml] Memory leak / cleanup missing after node creation.




Thanks. It is most unfortunate that I have to keep a list of the nodes, since if I free the node using xmlFreeNode before replacing it I lose the node I want to replace - if I free it afterwards I lose the node I replaced it with! I guess I'll have to keep a list of newNode's that I have assigned, so I can free them later after I have finished with the doc.

Regards,
                Jose.

On 22 May 2004, at 9:38 am, Igor Zlatkovic wrote:

On 20.05.2004 21:49, Jose Commins wrote:

(libxml2 - 2.6.10)
Hi, I am observing a leak when performing this walk/replace through nodes:
static void process_XML_elements(xmlNode *a_node)
{
xmlNode *newNode = NULL;
xmlNode *cur_node = NULL;
        for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        switch(cur_node->type)
        {
            case XML_ELEMENT_NODE:
                if(!strcmp("aTest", cur_node->name))
                {
                    newNode = xmlNewText(theTagReplacements[1]);
                    xmlReplaceNode(cur_node, newNode);
                }
            break;
            default:
            break;
        }
        process_XML_elements(cur_node->children);
    }
}
'xmlFreeDoc' clears up the doc allocation but not the nodes assigned above to replace the previous nodes. A kludge would be to maintain a list of allocated nodes then freeing them individually, but it would be nice if 'xmlFreeDoc' or similar freed all the nodes assigned to the doc itself.

Your cur_node is not linked to the document in any way after the call to xmlReplaceNode. This means that a call to xmlFreeDoc won't free that node. I don't know what you mean by "assigned to the doc", perhaps you meant "linked".

In every case, xmlReplaceNode unlinks the old node from the document and nothing in libxml's context knows about this node any longer. You are responsible for freeing it using xmlFreeNode.

Ciao,
Igor





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