Re: [xml] Memory leak / cleanup missing after node creation.
- From: Jose Commins <axora myrealbox com>
- To: Igor Zlatkovic <igor zlatkovic com>
- Cc: xml gnome org
- Subject: Re: [xml] Memory leak / cleanup missing after node creation.
- Date: Mon, 24 May 2004 16:51:59 +0100
Many thanks Igor!
The text node I am replacing it with is meant to remove all children
from the old node, and after reading your excellent explanations I see
where I have made the mistake - basically I didn't realise the old node
remained unlinked but not free, and that I was continuing processing
the old node thus causing the problems I was having. I replaced the
code below with:
...
newNode = xmlNewText(theTagReplacements[1]);
xmlReplaceNode(cur_node, newNode);
xmlFreeNode(cur_node);
cur_node = newNode;
...
I understand now that a replaced/unlinked node still 'hangs around'
and has to be freed.
The code now works without leaks - you have solved my problem. A
thousand thanks!
Regards,
Jose.
On 24 May 2004, at 4:19 pm, Igor Zlatkovic wrote:
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);
> }
> }
Ciao,
Igor
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]