Re: [xml] Can i use xmlUnlinkNode + xmlFreeNode to free a xmlNode in xmlNodeSetPtr of xmlXPathObjectPtr?

On Fri, Apr 17, 2009 at 03:54:18PM +0800, åæ wrote:

I just sustaining some codes used libxml.
The basic code logic is like this:

            xpathObj = xmlXPathEvalExpression((xmlChar*)xpathExpr, xpathCtx);
            for (i=0; i<xpathObj->nodesetval->nodeNr; i++) {
                xmlNodePtr newNode =
xmlAddPrevSibling(xpathObj->nodesetval->nodeTab[i], dataNode);
                if (newNode) {
                    newNode = xpathObj->nodesetval->nodeTab[i];
                    /* xpathObj->nodesetval->nodeTab[i] should be set NULL,
                       otherwise xmlXPathFreeNodeSet (called by
                       will access a unkonw memory */
                    xpathObj->nodesetval->nodeTab[i] = NULL;
            xmlXPathFreeObject(xpathObj );

you can suppose it's a update action: add the new node, and remove the old node.

I just wonder should i call xmlFreeNode to free old Node?
Or just unlink it, then xmlXPathFreeObject free it?

  xmlXPathFreeObject free only the object not any of the node which may
be pointed, with the only exception of namespace nodes where libxml2 and
XPath model didn't match.

It seems that xmlXPathFreeObject() call xmlXPathFreeNodeSet() to free
the node, this different with xmlFreeNode().
What's the correct way in this case.

  You need to unlink and free the node, and set the given entry to NULL
in the XPath nodeset.


Daniel Veillard      | libxml Gnome XML XSLT toolkit
daniel veillard com  | Rpmfind RPM search engine | virtualization library

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