Re: [xml] odd xpath troubles (xmlNodeSetPtr)



sramsay uga edu said:

I'm experiencing some really strange troubles with the xpath
facilities in libxml2.

"Strange", of course, is a relative term, closely related to the
observer's perceptions.

I wrote a simple program that uses xpath to grep out some text
content from a document.  The key function goes something like this:

xmlNodeSetPtr nodes;
xmlNodePtr      current;

xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
nodes = xpathObj->nodesetval;

int size = (nodes) ? nodes->nodeNr : 0;

for (int i = 0; i < size; i++) {
      current = nodes->nodeTab[i];
      printf("%s\n", current->content);
}

Brevity is admirable, but here there is a bit too much left to the
reader's imagination.  I would be particularly interested in the
eventual fate of the object "xpathObj", after the for loop has
completed.  Might I assume that it is delivered to
xmlXPathFreeObject for disposal at some point before the routine has
completed?

This compiles (and works) perfectly.  Upon reflection, though, it
occurred to me that I'd rather have this function return an
xmlNodeSetPtr to the caller and let the caller print out the nodes.
This entailed moving some of the above code out of the function and
into the caller, so now that caller does something like this:

xmlNodeSetPtr nodes;
xmlNodePtr      current;

nodes = get_raw_xml_nodes(filename, xpath);

int size = (nodes) ? nodes->nodeNr : 0;

for (int i = 0; i < size; i++) {
      current = nodes->nodeTab[i];
      printf("%s\n", current->content);
}

Here there is no longer any XPathObject in view, but the variable
"nodes" is pointing somewhere within it.  This presents me with a
choice: either I assume the previous routine left the object intact
(which would be poor programming, creating a possibly severe memory
"leak"), or I assume the previous routine properly freed the object,
and delivered back to the caller a pointer into some memory which
had been freed (which, might I be so bold as to say, would also be
classified as poor programming, but now with possibly disasterous
effects).

In other words, it does pretty much the same thing, except it grabs
the xmlNodeSetPtr from another function.  This also compiles without
a hitch.

Compilers are generally in the same class as beasts of burden.  The
fact that they don't complain should not be taken as proof that your
actions are exemplary .

When I run it, I get the same output as I did before, but with the
first four characters or so of each content node as junk characters
(the rest is fine).  Can anyone think of any possible reason why
this might be so?  Why would the xmlNodeSet be any different when
picked up as a return value from another function?


Could it be possible that the memory containing the node data has
been freed?  That often produces this symptom (and how could I
possibly be so familar with such facts......).

I tried searching through the archives, but the search function
seems to be busted at the moment.  My documents are in UTF-8.

Use the search engine at http://xmlsoft.org, where you can also find
full descriptions of the functions as well as example programs.

I'm thinking the answer might be "go learn C," but I don't think so.
I've been pouring over it for hours.

Steve

--
Stephen Ramsay
Assistant Professor
Department of English
University of Georgia
email: sramsay uga edu
web: http://cantor.english.uga.edu/
PGP Public Key ID: 0xA38D7B11

Again, it's difficult to make a good diagnosis with the limited
information you supplied.  If my above guesses are wrong, try
posting more details and other list members may have better ideas.

Regards,

Bill




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