azraelly / ticpp

Automatically exported from code.google.com/p/ticpp
0 stars 0 forks source link

i think there is a memoryleak in LinkEndChild(..) #42

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
A code like:

ticpp::Element* pElem=...doesnt matter..
ticpp::Element* element=new ticpp::Element(name);
ticpp::Text* elementtext=new ticpp::Text(text);
element->LinkEndChild(elementtext);
pElem->LinkEndChild(element);

doesnt free the memory when pElem is destroyed.

My english isnt well enough to descripe the problem any further so
i wrote a little testprogramm to show you the problem.
Its just a little function in a loop which adds some Elements to a
document. if you look on the memory the programm needs, you can see it growing.

I hope you understand what im trying to tell you :) if you have any
questions or i made a mistake just mail me...

Original issue reported on code.google.com by Mister_H...@hotmail.com on 5 Mar 2009 at 10:51

Attachments:

GoogleCodeExporter commented 9 years ago
I can confirm this.

When you use LinkEndChild on a node, it calls LinkEndChild for the TiXmlNode, 
which links the two TiXmlNodes together.

When the TiXmlNode is destroyed, it correctly deletes all child TiXmlNodes that 
were linked to it, so that's fine.

But there's no reference kept anywhere of the original ticpp::Node, so nothing 
ever deletes it.

Original comment by J.R.Hodg...@gmail.com on 7 Oct 2010 at 9:56

GoogleCodeExporter commented 9 years ago
Hello, 

i have the same problem as described. My testcode:

pXMLDoc = new Document();

/* create root element */
Element* pDocumentRootElement = new Element("MyRoot");
pXMLDoc->LinkEndChild(pDocumentRootElement);

Element* pMiscellaneous= new Element("MISC");
pDocumentRootElement->LinkEndChild(pMiscellaneous);

pMiscellaneous->LinkEndChild(new Element("tttt", 232));  //not deleted !!!

delete pDocumentRootElement;
delete pXMLDoc;

At the end of this code i have a memory leak. The memory of "...new 
Element("tttt", 232));  " is not deleted.

What can I do?  Have anyone a solution for this problem ? The documentation of 
LinkEnd Child say, that this memory will be deleted after delete of his parent 
child.

Original comment by i...@christiansimon.de on 18 Mar 2011 at 4:51

GoogleCodeExporter commented 9 years ago
Hi,

I think this is a more general problem. Reference counters are initialized to 1 
via InitRef(), but when you just allocated the object noone points to it and 
the counter should be 0.
I use another smart pointer framework (The one in OpenSceneGraph), and counters 
are initialized to 0. And of course it works well. I believe the initialization 
to 1 is problematic.

Thoughts?

Original comment by sukend...@gmail.com on 24 Mar 2011 at 11:00

GoogleCodeExporter commented 9 years ago
I found this in the documentation...
"There is no reason to create TinyXML++ objects on the heap, using new, because 
the memory is managed for you. If you choose to use new to create TinyXML++ 
objects, you will always need to use delete to clean up."
http://ticpp.googlecode.com/svn/docs/ticpp.html

So maybe that explains the leak, but doesn't this make LinkEndChild() rather 
useless? What would be the proper usage for that function?

Original comment by ryan.da...@gmail.com on 4 Apr 2011 at 3:32

GoogleCodeExporter commented 9 years ago
Agreed... LinkEndChild() is supposed to attach a node allocated on user's side, 
as far as I know.

Original comment by sukend...@gmail.com on 11 Apr 2011 at 10:45

GoogleCodeExporter commented 9 years ago
Hi,

Did anyone find a resolution to the memory leak issue?

Thanks

Original comment by shomadi...@gmail.com on 21 Oct 2013 at 10:15

GoogleCodeExporter commented 9 years ago
The issue is that ticpp::Element::Element does a 'new' on TiXmlElement, thus 
doing 'new' on ticpp::Element is redundant.  The effect is as if LinkEndChild 
made a copy of the ticpp::Element object.

So where I used to have :
   element->LinkEndChild(new ticpp::Element("foo"));

I now have:
   ticpp::Element child("foo");
   element->LinkEndChild(&child);

Everything works fine and no more leakage.  Thanks to ryan (comment #4) for the 
document ref.

Original comment by rldu...@gmail.com on 17 May 2014 at 10:41