[xml] memory leak while parsing from memory buffer



Hi Daniel, All,

in our Perl bindings, we have something similar to this (we keep
compatibility with older releases, that's why we still use the old
parser API):


xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt((const char*)ptr, len);

/* leak here - comment out to get rid of it */
if ( ctxt->input != NULL ) {
   ctxt->input->filename = xmlStrdup((const xmlChar *) "");
} 

xmlParseDocument(ctxt);

real_doc = ctxt->myDoc;
ctxt->myDoc = NULL;

xmlFreeParserCtxt(ctxt);

return real_doc;


Now, the reason we set ctxt->input->filename to "" is that otherwise
libxml2 (since 2.6) doesn't return line-numbers on error - we want
something like

:3: element bar: validity error : No declaration for element bar
<bar/>
      ^

but if we leave filename == NULL, we only get 

element bar: validity error : No declaration for element bar
<bar/>
      ^

(No problem with this.)

But, although ctxt->input->filename _does_ get freed with
xmlFreeParserCtxt, the above code leaks. If the line with xmlStrdup is
removed, the leak disappears. I suspect, that somewhere, during
xmlParseDocument some copies of filename are made but never freed or
something similar. Otherwise I don't know why this line would make any
difference. BTW, it's the same if I set

ctxt->input->filename = "";

and put 

ctxt->input->filename = NULL;

just before xmlFreeParserCtxt(ctxt).

Any hints?

Thanks,

-- Petr

Attachment: pgpeISeh01zCa.pgp
Description: PGP signature



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