leethomason / tinyxml2

TinyXML2 is a simple, small, efficient, C++ XML parser that can be easily integrated into other programs.
zlib License
5.11k stars 1.84k forks source link

XMLPrinter bug? #733

Open alexander-doroshenko opened 5 years ago

alexander-doroshenko commented 5 years ago

Hi, I get an exception is thrown in the destructor of XMLPrinter after printing a document to string. Here is an example code:

    tinyxml2::XMLPrinter printer;
    {
        tinyxml2::XMLDocument doc{};
        for ( int i = 1000; i--; )
        {
            doc.InsertEndChild(doc.NewElement("hello my dear friends"));
        }
        doc.Accept(&printer);
        std::cout << printer.CStr();
    }
    std::cout << "XMLDoc destroyed";
Gumichan01 commented 5 years ago

Can you give us more details?

I tried to reproduce the bug by using the example code you provided (library at 8f4a9a8):

#include "tinyxml2.h"
#include <iostream>

int main(int argc, char const *argv[]) 
{
    tinyxml2::XMLPrinter printer;
    {
        tinyxml2::XMLDocument doc{};
        for ( int i = 1000; i--; )
        {
            doc.InsertEndChild(doc.NewElement("hello my dear friends"));
        }
        doc.Accept(&printer);
        std::cout << printer.CStr();
    }
    std::cout << "XMLDoc destroyed";
    return 0;
}

I compiled this by using g++ example.cpp libtinyxml2.a. I couldn't reproduce.

alexander-doroshenko commented 5 years ago

Sorry for my incompetence. I use VS2017 (v141 toolset) on Windows OS. My investigation showed that an error appears if size of result string of XmlPrinter more than 20 (size of static array of a DynArray class) and I simplified the example:

#include <iostream>

#include "tinyxml2.h"
//
int main()
{
    {
    tinyxml2::XMLPrinter printer;
    printer.OpenElement("Hello my dear friends");
    printer.CloseElement();
    std::cout << printer.CStr();
    }
    std::getchar();
    return 0;     
}

I compiled this by using cl /EHsc /MDd TestTinyXML2.cpp "tinyxml2.lib"

When I first caught the error I used VS2015 (v140 toolset) but now with that toolset I cannot reproduce error too.

goodjian7 commented 5 years ago

I meet similar issue today. I hope my experience can help you to solve problem. I built library with visual studio 2010. but i linked it visual studio 2015 project. so, build tool set was 140 which is different from one that i used for building library (v100). In this case, crash related to memory releasing was occurred whenever XMLPrinter was destructed. But, after i built my project visual studio 2010, the problem disappeared. i think it is because i build my project with same build tool i used for building library.