Re: [xml] Is this condition (xmlFreeNodeList) supposed to crash?




On 30 Nov 2004, at 14:54, Tomas Sieger wrote:

Jose Commins wrote:
    If I perform this:
    output_root_element = xmlDocGetRootElement(output_doc);
    xmlFreeNodeList(output_root_element);
    xmlFreeDoc(output_doc);
    I get a crash.
If I remove 'xmlFreeNodeList' it doesn't crash. Now, I don't normally use 'xmlFreeNodeList' but I've seen many examples on this list that do so, therefore I put in the line of code just to see what happened. In my XML doc I have nodes attached to 'output_doc' that I've created. Is the above code supposed to crash or is there something I am not aware of?
Regards,
        Jose.

Read the documentation. Well, xmlFreeNodeList() does not say a lot, but when you look at xmlFreeNode, it says:

  "This doesn't unlink the child from the list,
   use xmlUnlinkNode() first."


        I do read the documentation :o)

There is some work to do on the documentation, quoting the above example, to compare:


        Function: xmlFreeNode
        void    xmlFreeNode                     (xmlNodePtr cur)

Free a node, this is a recursive behaviour, all the children are freed too. This doesn't unlink the child from the list, use xmlUnlinkNode() first.

        cur:
        the node

        ...with...

        Function: xmlFreeNodeList
        void    xmlFreeNodeList                 (xmlNodePtr cur)

Free a node and all its siblings, this is a recursive behaviour, all the children are freed too.

        cur:
        the first node in the list


... to me its not clear what the precise difference between these two are. Moreover, xmlFreeNodeList does not mention anything about xmlUnlinkNode.


        What's more, this is particularly puzzling:


        Function: xmlCleanupMemory
        void    xmlCleanupMemory                (void)

        Free up all the memory associated with memorys


So does this mean it frees up all memory including new nodes created with xmlNewDocText, for example? It doesn't say.


So, your problem is that you try to free the nodes twice: once using xmlFreeNodeList(), second via xmlFreeDoc(). Do not call xmlFreeNodeList() unless you also unlink the nodes from the tree.

hope it helps
 Tomas


Aye, that I understand, and in my app that is what I normally don't do. Since I've seen a few examples quoted by people that did use the above method I was curious to try in case I did not take into account things not mentioned in the documentation :o)


Regards,
                Jose.


--
Experience, n.: Something you don't get until just after you need it. -- Olivier




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