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

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) {
            case XML_ELEMENT_NODE:
                if(!strcmp("aTest", cur_node->name))

                    newNode = xmlNewText(theTagReplacements[1]);
                    xmlReplaceNode(cur_node, newNode);





'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.


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