Crazywonton
Crazywonton

Reputation: 41

Libxml2 - Memory Leak Renaming Namespace Prefixes

I'm working on renaming namespace prefixes in XML documents using Libxml2 for C. The code I have here is where the renaming takes place. It gets the job done but Valgrind is reporting a small memory leak from the original namespace ptr that was allocated via xmlParseDocument(doc).

    xmlNsPtr def(xmlSearchNs(NULL, node, nsptr->prefix));
    const char* myPrefix = "foo";
    def->prefix = (xmlChar*) myPrefix;

Calling xmlFreeNs(nsptr) results in a SIGABRT from an invalid free. Calling free(nsptr) has no affect. Calling xmlFreeNsList(nsptr) or delete(nsptr) reduce the size of the memory leak... but segfault.

I have not found any examples of prefix manipulation with Libxml2, and the API documentation for Libxml2 does not offer a function for this, so I'm just about iterating through possibilities at this point. Help is much appreciated.

Upvotes: 1

Views: 137

Answers (1)

Crazywonton
Crazywonton

Reputation: 41

[Update] So after lots of trial and error, the solution I found which works is a combination of what nwllnhof described and a code snippet found here on changing/removing namespaces.

xmlNsPtr def(xmlSearchNs(NULL, node, nsptr->prefix));
xmlFree((void*) def->prefix);
def->prefix = xmlStrdup((const xmlChar*) "foo");

Upvotes: 1

Related Questions