Sign in to follow this  
Mybowlcut

TinyXML++ memory leaks

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
How did you compile your tinyxml project?
I've been working on a smaller (and simpler) version of Visual Leak Detector (mostly for learning purposes) and while I did this I noticed that if I create a project as a static library and link it with another executable, the overwritten new and delete functions of my leak detector were called in a wrong way (delete[] in a module in the static lib was redirected to delete (without [] brackets) in my leak detector) or not at all. This behaviour vanished when I linked the leak detector against the static library as well as against the executable.

If you've compiled tinyxml as a dll, then you need to link visual leak detector to that one as well (or it won't be able to detect the deallocation of the 'Element'), although you propably already know that.

As far as I know, the leak detector works in a similar way (overwriding operator new, and so on) and I assume it could suffer from the same problems as mine did.

Hope that helps :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this