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

On 24/05/04 16:33, Jose Commins wrote:

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!

You cannot free a node before you replace it or unlink it from the document somehow. It would be like removing a link from a chain and expecting the chain to be whole afterwards.

The node you replaced it with should be in the document tree, where the old node was.

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.

I think you have a different problem. In your code, you are replacing an element node with a text node. Element nodes can have children, text nodes cannot, so the new text node does not adopt the children of the old element node. Few lines down the road, you process the children of the old element node, means process a fragment which is now outside your document tree. The old element node does not have cur_node->next anymore, that link now belongs to the new text node.

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


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