Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

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

Open
GoogleCodeExporter opened this issue Mar 17, 2015 · 7 comments
Open

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

GoogleCodeExporter opened this issue Mar 17, 2015 · 7 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 5 Mar 2009 at 10:51

Attachments:

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 7 Oct 2010 at 9:56

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 18 Mar 2011 at 4:51

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 24 Mar 2011 at 11:00

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 4 Apr 2011 at 3:32

@GoogleCodeExporter
Copy link
Author

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

Original comment by [email protected] on 11 Apr 2011 at 10:45

@GoogleCodeExporter
Copy link
Author

Hi,

Did anyone find a resolution to the memory leak issue?

Thanks

Original comment by [email protected] on 21 Oct 2013 at 10:15

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 17 May 2014 at 10:41

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant