Re: [xml] exception/segfault on xmlFreeDoc()



A general suggestion (having written a lot of memory management over the years) is to replace "free" with some other routine (say "my_free") throughout. This routine would take the address of the pointer rather than the pointer ... then it becomes a simple routine:

int my_free(void **ptr)
{
        if (!*ptr)
                return(0);
        free(*ptr);
        *ptr = NULL;
        return(1);
}

The return code tells if it was actually free'd ... for debugging purposes sometimes useful.

This tends to generically solve the problem of double frees as well as the problem of accessing memory that has been freed.

Something to consider for future release :-)


At 10:54 AM 3/10/04, you wrote:

When I run test_LoadDocument() I get a exception/segfault in the
xmlFreeDoc() after the successful LoadDocument.

With MSVC++ and libxml2-2.6.5 it crashes in dict.c line 709, because
dict has already been free'd.

The libxml2 has been linked statically and was compiled with teh
following configuration and defines:

cscript configure.js xml_debug=no debug=no static=yes iconv=no
schemas=no regexps=no docb=no threads=native ftp=no http=no reader=no
walker=no legacy=no xpath=yes xptr=no catalog=no xinclude=no c14n=no
cruntime=/MT

LIBXML_STATIC / LIBXML_STATIC_FOR_DLL / _REENTRANT / HAVE_WIN32_THREADS

And trying an older version (I don't have the version number handy) in
MinGW also crashes. The crash does happen in valid.c line 2856
(xmlGetDtdAttrDesc() -> if (dtd->attributes == NULL) return(NULL);)
because dtd has already been free'd.

I looked all over it and couldn't see the problem at all. It all looked
fine to me. I unlinked the nodes properly.

Here is the code:

bool CreateXMLDocument(xmlDocPtr* ppDoc)
{
    try
    {
        if (ppDoc == NULL) return false;

                *ppDoc = NULL;

                *ppDoc = xmlNewDoc(BAD_CAST "1.0");

                if( *ppDoc != NULL )
                        return true;
    }
    catch (...) { }
        return false;
}

int CreateLoadXMLDoc(xmlDocPtr* ppDoc, const char* Filename)
{
    try
    {
            if (ppDoc == NULL) return -1;
            *ppDoc = NULL;

            // Create XML document
            xmlDocPtr pXMLDoc = NULL;
            if (!CreateXMLDocument(&pXMLDoc))
                    return -1;

            // Load XML file, if a file name is given
            if (Filename)
            {
                        pXMLDoc = xmlParseFile(Filename);

                        if( pXMLDoc == NULL )
                                return -1;
            }

            *ppDoc = pXMLDoc;

            return 0;
    }
    catch (...)
    {
        return -1;
    }
}

bool LoadDocument(const char* Filename, xmlDocPtr pDoc)
{
        xmlDocPtr doc = NULL;
        xmlNodePtr root = NULL, new_root = NULL;

        root = pDoc->children;

        if( root ) {
                xmlUnlinkNode(root);
                xmlFree(root);
        }

        if( CreateLoadXMLDoc(&doc, Filename) != 0 )
                return false;

        new_root = doc->children;

        if( new_root == NULL )
                return false;

        xmlUnlinkNode(new_root);

        if( doc )
                xmlFreeDoc(doc);

        xmlDocSetRootElement(pDoc, new_root);

        if( pDoc == NULL )
                return false;

        return true;
}

bool test_LoadDocument()
{
        xmlDocPtr doc = NULL;

        if( !CreateXMLDocument(&doc) ) {
                return false;
        }

        if( LoadDocument("_pluginList.xml", doc) ) {
                xmlFreeDoc(doc);
                return true;
        }

        xmlFreeDoc(doc);

        return false;
}
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml

Eric S. Eberhard
(928) 567-3727          Voice
(928) 567-6122          Fax
(800) 569-1122 Denver Office (I am never there, you can leave a message)
(720) 339-4765          Cell

http://www.vicspdi.com

Completely updated web site of personal pictures with many new pictures! Includes horses, dogs, Corvairs, and more.

http://www.vicspdi.com/ourpics/index.html




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