C++, libxslt: Freeing the stylesheet document after freeing the style sheet results in crash

I'm using libxml2 with libxslt for XML processing from a C++ program. For transforming XML documents with XSL, I use the following function (error handling removed):

xmlDocPtr
transformXmlDocument(
    const xmlDocPtr inputDocument,
    const std::string& stylesheetString
    ) {

    exsltRegisterAll();

    // Read the stylesheet document.
    xmlDocPtr stylesheetDocument = xmlReadMemory(
            stylesheetString.c_str(),
            stylesheetString.length(),
            "stylesheet.xsd",
            0, // No encoding set - get it from the file header.
            0  // No further options.
    );

    // Parse the stylesheet
    xsltStylesheetPtr stylesheet = xsltParseStylesheetDoc(stylesheetDocument);

    // Transform the document
    xmlDocPtr result = xsltApplyStylesheet(stylesheet, inputDocument, 0);

    // Free used resources
    xsltFreeStylesheet(stylesheet);
    xsltCleanupGlobals();

    // Here the program crashes
    xmlFreeDoc(stylesheetDocument);

    return result;
}

The problem is that the program crashes with an access violation (glibc says: free(): invalid pointer: 0x00000000026d8090 *) in the second last line.

I can't find any hints in the docs that xsltFreeStylesheet also frees the underlying document or something, so what is wrong here?

Answers


xsltFreeStylesheet also frees the underlying document or something

The fine manual has some hints which would suggest that there's indeed a chance of this scenario happening.


Need Your Help

boost::thread: How to start all threads, but have only up to n running at a time?

c++ multithreading boost

In the boost::thread library, is there any mechanism to control how many threads (at most) are running at a time?

jQuery(html, props) not working on an input

jquery dom-manipulation

The jQuery overload for creating an HTML element and supplying a map of properties does not seem to work for controls. I saw in the documentation that IE does not allow you to change the type so you