Re: [xml] Retrieving boolean/number/string value from XPath objects



Le 14/05/01 21:19:53, Daniel Veillard a écrit :
  The code is nice, thanks a lot, recompiled, I found just one
small bit about xmlXPathCastNodeSetToBoolean() which is not static
but not exported either. It should probably be added to xpath.h

  The problem came when recompiling libxslt against it and running
the tests. I detected some memory leaks. I didn't found where exactly,
what I suspect is that there has been a small change in one of the
API (seems related to ID lookups) which generates this leak,

  I will try to find it and will commit this once found,

Found!

tokens isn't freed in the loop in xmlXPathIdFunction.

Also, I used xmlConvertString in the case the argument isn't a node-set. I
should have used tokens = xmlXPathCastToString(obj); and then, to free both
obj and tokens without memory leak:
  if (obj->type != XPATH_STRING) {
    /* another possibility is tokens != obj->stringval */
    xmlFree(tokens);
  }
  xmlXPathFreeObject(obj);

I'll make a piece of the interface I explained in "XPath extension API" so
that the code will become something like this:
void
xmlXPathIdFunction (... ctxt, int nargs);
  ...
  if (nargs != 1) {
    xmlXPathSetInvalidArity(ctxt);
    return;
  }
  if (xmlXPathStackType(ctxt) == XPATH_NODESET) {
    ...
    obj = xmlXPathPopNodeSet(ctxt);
      /* here comes the loop */
    xmlXPathFreeNodeSet(obj);
  } else {
    tokens = xmlXPathPopString(ctxt);
    ret = xmlXPathGetElementsByIds(xmlXPathGetDocument(ctxt), tokens);
    xmlFree(tokens);
  }
  xmlXPathReturnNodeSet(ret);
}

No need to burden ourselves with xmlXPathObjects when we don't need them.

Tom.




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