tinyxml2 icon indicating copy to clipboard operation
tinyxml2 copied to clipboard

XMLPrinter bug?

Open alexander-doroshenko opened this issue 6 years ago • 3 comments

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";

alexander-doroshenko avatar Jan 31 '19 07:01 alexander-doroshenko

Can you give us more details?

  • Operating system
  • Compiler (command used, options)
  • Step to reproduce

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.

Gumichan01 avatar Feb 11 '19 23:02 Gumichan01

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.

alexander-doroshenko avatar Feb 14 '19 09:02 alexander-doroshenko

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.

goodjian7 avatar Apr 04 '19 04:04 goodjian7