TinyXML++ memory leaks

Started by
9 comments, last by SiS-Shadowman 15 years, 3 months ago
Hey. Visual Leak Detector is telling me that I've got memory leaks coming from code that uses ticpp (TinyXML++) code:
void CGE_Level::Write_Tiles(ticpp::Element* root) const
{
	using namespace ticpp;

	Element* tiles = new Element("tiles");
	Element* tile = NULL;
	root->LinkEndChild(tiles);
	for(const_tile_it it = this->tiles.begin(); it != this->tiles.end(); ++it)
	{ // Write each background tile.
		tile = new Element("tile");
		tile->SetAttribute("type", it->Type());
		tile->SetAttribute("pass", it->Passability());
		tiles->LinkEndChild(tile);
	}
}

void CGE_Level::Write_Portals(ticpp::Element* root) const
{
	using namespace ticpp;
	
	Element* portals = new Element("portals");
	Element* portal = NULL;
	root->LinkEndChild(portals);
	for(const_portal_it it = this->portals.begin(); it != this->portals.end(); ++it)
	{ // Write each background tile.
		portal = new Element("portal");
		portal->SetAttribute("to_level", it->To_Level());
		portal->SetAttribute("from_x", it->From_Pos().y);
		portal->SetAttribute("from_y", it->From_Pos().y);
		portal->SetAttribute("to_x", it->To_Pos().x);
		portal->SetAttribute("to_y", it->To_Pos().y);
		portals->LinkEndChild(portal);
	}
}

void CGE_Level::Write(const std::string& file_name) const
{
	using namespace ticpp;

	Document xml_doc(file_name);

	Declaration* dec = new Declaration(DEF_VER, DEF_ENC, DEF_STA);
	xml_doc.LinkEndChild(dec);

	Element* root = new Element("level");
	xml_doc.LinkEndChild(root);
	root->SetAttribute("width", size.x);
	root->SetAttribute("height", size.y);

	Write_Tiles(root);
	Write_Portals(root);

	xml_doc.SaveFile(file_name);
}
Quote:WARNING: Visual Leak Detector detected memory leaks! ---------- Block 4309 at 0x0051CC78: 8 bytes ---------- Call Stack: c:\tinyxml++\ticpp.cpp (1089): TiCppRC::TiCppRC c:\tinyxml++\tinyxml.h (217): TiXmlBase::TiXmlBase c:\tinyxml++\tinyxml.cpp (136): TiXmlNode::TiXmlNode c:\tinyxml++\tinyxml.cpp (508): TiXmlElement::TiXmlElement c:\tinyxml++\ticpp.cpp (874): ticpp::Element::Element c:\documents and settings\bill\my documents\visual studio 2005\projects\console_game_engine\console_game_engine\cge_level.cpp (103): CGE_Level::Write_Portals c:\documents and settings\bill\my documents\visual studio 2005\projects\console_game_engine\console_game_engine\cge_level.cpp (135): CGE_Level::Write c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (145): CGE_Editor::Save c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (56): CGE_Editor::Update_And_Draw c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (46): CGE_Editor::Run c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\main.cpp (72): main f:\rtm\vctools\crt_bld\self_x86\crt\src\crtexe.c (586): __tmainCRTStartup f:\rtm\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): mainCRTStartup 0x7C817067 (File and line number not available): RegisterWaitForInputIdle Data: 01 00 00 00 00 00 00 00 ........ ........ ---------- Block 4307 at 0x0051CB20: 12 bytes ---------- Call Stack: c:\documents and settings\bill\my documents\visual studio 2005\projects\console_game_engine\console_game_engine\cge_level.cpp (103): CGE_Level::Write_Portals c:\documents and settings\bill\my documents\visual studio 2005\projects\console_game_engine\console_game_engine\cge_level.cpp (135): CGE_Level::Write c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (145): CGE_Editor::Save c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (56): CGE_Editor::Update_And_Draw c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (46): CGE_Editor::Run c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\main.cpp (72): main f:\rtm\vctools\crt_bld\self_x86\crt\src\crtexe.c (586): __tmainCRTStartup f:\rtm\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): mainCRTStartup 0x7C817067 (File and line number not available): RegisterWaitForInputIdle Data: 0C B0 4C 00 78 CC 51 00 68 CB 51 00 ..L.x.Q. h.Q..... ---------- Block 4291 at 0x0051C820: 8 bytes ---------- Call Stack: c:\tinyxml++\ticpp.cpp (1089): TiCppRC::TiCppRC c:\tinyxml++\tinyxml.h (217): TiXmlBase::TiXmlBase c:\tinyxml++\tinyxml.cpp (136): TiXmlNode::TiXmlNode c:\tinyxml++\tinyxml.cpp (508): TiXmlElement::TiXmlElement c:\tinyxml++\ticpp.cpp (874): ticpp::Element::Element c:\documents and settings\bill\my documents\visual studio 2005\projects\console_game_engine\console_game_engine\cge_level.cpp (92): CGE_Level::Write_Tiles c:\documents and settings\bill\my documents\visual studio 2005\projects\console_game_engine\console_game_engine\cge_level.cpp (133): CGE_Level::Write c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (145): CGE_Editor::Save c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (56): CGE_Editor::Update_And_Draw c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\cge_editor.cpp (46): CGE_Editor::Run c:\documents and settings\bill\my documents\visual studio 2005\projects\console_level_editor\console_level_editor\main.cpp (72): main f:\rtm\vctools\crt_bld\self_x86\crt\src\crtexe.c (586): __tmainCRTStartup f:\rtm\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): mainCRTStartup 0x7C817067 (File and line number not available): RegisterWaitForInputIdle Data: 01 00 00 00 00 00 00 00 ........ ........ ETC...
In total there are 208! I didn't delete the pointers I created here because I followed the tutorial and they didn't use delete. I read somewhere that TinyXML++ handles the memory internally... but when I saw vld reporting the memory leaks, I realised I had stuffed up and that I hadn't actually deleted the pointers... pretty stupid I know. So two questions: 1) What does TinyXML++ do in terms of handling new'd memory? 2) Why would they not delete the pointers in their tutorials? Cheers!

Advertisement
It looks more likely to me that your leak detector is wrong. Once you link a node into an XML document it would be foolish to expect you to have to delete it manually. But I've not looked at the code myself.
Quote:Original post by Kylotan
It looks more likely to me that your leak detector is wrong. Once you link a node into an XML document it would be foolish to expect you to have to delete it manually. But I've not looked at the code myself.
Hmm.. well I deleted the pointers after each new and the leaks went away... I still am curious about it all though.

Quote:Original post by Kylotan
It looks more likely to me that your leak detector is wrong. Once you link a node into an XML document it would be foolish to expect you to have to delete it manually. But I've not looked at the code myself.
On the other hand, it seems silly that the XML document must be allocated in such a way that the standard single-object no-parameter delete will properly deallocate them. Perhaps there are additional optional arguments, one of which is a deallocator instance, but somehow I doubt it.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Gah! We've been using TinyXML at work and haven't been deleting any of the allocated memory. We were under the assumption that when the document went out of scope, it would automatically delete any linked nodes.

But on the third hand, it is possible for you to link a node object that's on the stack, in which case deleting it would crash. In that case, the usage guide in the documentation just plain sucks for not making any of this clearer :)
Quote:Original post by Extrarius
Quote:Original post by Kylotan
It looks more likely to me that your leak detector is wrong. Once you link a node into an XML document it would be foolish to expect you to have to delete it manually. But I've not looked at the code myself.
On the other hand, it seems silly that the XML document must be allocated in such a way that the standard single-object no-parameter delete will properly deallocate them.

Why? The semantics of TinyXML clearly imply that (a) it owns the document tree, and (b) you pass in nodes that you allocate. I can see no good argument for it not simply using the delete operator on everything linked into it.

EDIT: In fact, given that TinyXML (or at least the copy I have here) clearly has
while ( node ) { temp = node; node = node->next; delete temp; }
in the TiXMLNode destructor, I'm guessing this is just a bug in the TinyXML++ wrapper.

[Edited by - Kylotan on December 17, 2008 5:12:29 AM]
Still a bit confused... I'm guessing that I was right and I should have been deleting the pointers?

My theory is that the TinyXML++ wrapper has a bug, because I don't think you should be freeing anything that you have linked into the document.
By looking at the bugtracker ( http://code.google.com/p/ticpp/issues/detail?id=17&can=1&q=memory ) it seems a memory leak was fixed in revision 93.

But if there are more leaks, I really need to find out how to work around them, before we ship the software I'm currently working on.

On which version of ticpp are you seeing the problem?

- michael
I updated my version to revision r98 from ticpp svn (see http://code.google.com/p/ticpp/source/checkout ).

Valgrind on Linux does not show any leaks using this version.

- michael

This topic is closed to new replies.

Advertisement