Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeing xmlChar pointers after xmlTextReaderGetAttribute()

Tags:

c++

c

free

libxml2

I was using xmlTextReaderGetAttribute (from xmlsoft.org) successfully before, but the API documentation requires that I deallocate the returned xmlChar*. Now my app crashes on the second (the first passes null) call to free(), shown below:

xmlTextReaderPtr reader = null;
xmlChar *attribVal = null;
//blah...
if (xmlTextReaderAttributeCount(reader) > 0) {
    free((attribVal));

attribVal = xmlTextReaderGetAttribute(reader, (const xmlChar*)"super-Attrib");
if (xmlStrcasecmp(attribVal, (const xmlChar*)"monoMega-Attrib") == 0) {
    free((attribVal));

my project is in C++ but the libxml2 and all examples from xmlsoft.org use standard C.

like image 294
John Avatar asked Sep 21 '26 16:09

John


1 Answers

Use xmlFree() instead of free() directly:

xmlTextReaderPtr reader = null; 
xmlChar *attribVal = null; 
//blah... 
if (xmlTextReaderAttributeCount(reader) > 0)
{ 
    attribVal = xmlTextReaderGetAttribute(reader, BAD_CAST "super-Attrib"); 
    if (attribVal)
    {
        ...
        xmlFree(attribVal);
    }
} 
like image 84
Remy Lebeau Avatar answered Sep 24 '26 06:09

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!