Re: [xml] Possible memory leak in extension functions

On Tue, Feb 18, 2003 at 04:09:15PM +0000, Matt Sergeant wrote:
Now I *think* the culprit here is the fact that xmlXPathFreeNodeSet()
doesn't free the nodes (so how do you free them when this is a callback? -
you can't). But the docs for xmlXPathFreeNodeSetList() imply that
xmlXPathFreeObject *does* free the nodes.

    Well xmlXPathFreeNodeSetList() documentation is buggy.

  Welcome to C without garbage collection language trying to implement
specs that were only tested with Java prior going to REC :-)
  Basically the XPath engine never copy nodes, it just points at
nodes from the tree it was pointed at [*]. So nodeset content is not
deallocated, that would kill all previous use of XPath and XSLT.
  There is however a way to achieve what you want:
xmlXPathFreeObject(xmlXPathObjectPtr obj) {
    if (obj == NULL) return;
    if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
        if (obj->boolval) {
            if (obj->user != NULL) {
                xmlFreeNodeList((xmlNodePtr) obj->user);
            } else if (obj->nodesetval != NULL)
        } else {
            if (obj->nodesetval != NULL)

  if, on an XPATH_NODESET object you set obj->boolval to 1, then
assuming also that you have groupped the nodes built for that nodeset
back into a container node, then setting obj->user to that tree will
in effect free up the whole tree when the nodeset is deallocated. Or
if that user field isn't set the xmlXPathFreeValueTree() will do the 
deallocation for all objects referenced.
  But ... if the inner XPath computation leads to references to those
nodes then serious troubles may result from this. The key point is that
libxml2 XPath does not control the life of the referenced objects,
only the controlling layer does. And yes extension functions and
Result Value Trees have made my life miserable for a while, due
to those allocation problems.


Daniel Veillard      | Red Hat Network
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

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