tinyxml2
tinyxml2 copied to clipboard
Documentation of ownership misleading
Hi,
The documentation for the ownsership of nodes (in the main page, under features and memory management) states the following:
"However, any sub-node of the Document, XMLElement, XMLText, etc, can only be created by calling the appropriate XMLDocument::NewElement, NewText, etc. method. Although you have pointers to these objects, they are still owned by the Document. When the Document is deleted, so are all the nodes it contains."
When I first read it I thought that XMLDocument::New* made the node owned by the document. However, it is not owned until XMLDocument::Insert* is called, otherwise you get memory leaks.
Perhaps stating that the nodes are owned by the XMLDocument after they are inserted to the document should be added, if that is the intention.
Actually, you first impression was correct. The New* makes the node owned by the Document. If you delete the Document, the memory will get cleaned up. The memory is stored in memory pools the Document.
It does assert because you created memory you didn't use - I may need to remove the assert, it's confusing - but the memory isn't leaked.
Hi Lee,
Thanks for the explanation. But there do appear to be some memory leaks, unless I've got a false positive due to some kind of static initialisation / destruction. Running the following code (compiled as 32 bit binary in VC2008 on Win 7 x64):
#include <iostream>
#include <string>
#include "..\..\..\tinyxml2.h"
using namespace std;
using namespace tinyxml2;
int main(void)
{
{
XMLDocument doc;
XMLElement *eFoo;
doc.NewDeclaration();
eFoo = doc.NewElement("foo");
}
_CrtDumpMemoryLeaks();
return 0;
}
Results in the following output.
Detected memory leaks!
Dumping objects ->
{110} normal block at 0x00122B08, 4 bytes long.
Data: <foo > 66 6F 6F 00
{108} normal block at 0x00127B50, 35 bytes long.
Data: <xml version="1.0> 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30
Object dump complete.
The program '[7792] tests.exe: Native' has exited with code 0 (0x0).
If the allocations were owned by the document then they should be freed up at the first closing brace.
It looks like the memory allocations for the children are being freed from the memory pools but only the memory is being freed, the destructors are not being called. Since the children allocate heap memory for their strings and their destructors are not executed in order to free those allocations, the strings are being leaked.
Adding "bug" status.