[xml] Possible memory leak in extension functions

XML::LibXSLT 1.53 added better support for perl extension functions,
including the ability to return nodes and nodelists.

When a nodelist gets returned we have to turn it from a perl data
structure to a libxml2 xmlXPathObjectPtr (of type XPATH_NODESET). So we
iterate through the list returned, copy the nodes and free the original,
and append it to a new NodeSet object. Finally we valuePush() this onto
the return context. This seems like the right thing to do judging by the
documentation, sparse as it is.

Here's the relevant snippet of code. Sorry I haven't extracted this into a
test bit of code, but as I'm sure you can understand - XML::LibXSLT is a
rather large bit of work, so I've done my absolute best to ensure that its
not a leak in my module:

  ret = xmlXPathNewNodeSet(NULL);
  array_result = (AV*)SvRV(perl_result);
  while (av_len(array_result) >= 0) {
    tmp_node1 = x_PmmSvNode(sv_2mortal(av_shift(array_result)));
    // tmp_node = xmlDocCopyNode(tmp_node1, ctxt->context->doc, 1);
    // xmlXPathNodeSetAdd(ret->nodesetval, tmp_node1);
    // xmlFreeNode(tmp_node);
    xmlXPathNodeSetAdd(ret->nodesetval,xmlNewNode(NULL, "foo"));
  valuePush(ctxt, ret);

Note how the real code is commented out, and I've replaced it with some
code that creates an entirely new element <foo> - this was to ensure I
wasn't leaking clone structures for some reason.

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.

Looking at the code it looks like I want xmlXPathFreeValueTree to be
called, and in order to get that I have to have a true value in
->boolval, and NULL in ->user. Unfortunately setting those things up
didn't change anything :-/e

So, umm, what can I say but "help". Where's the leak?

<!-- Matt -->
<:->get a SMart net</:->
Spam trap - do not mail: spam-sig spamtrap messagelabs com

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