Question about DOM* class createXXX methods in C++. Do I have to do anything special to free memory returned from DOM*开发者_高级运维 createXXX methods?
For example (the transcodes were removed for simplification and the associated releases for the vars associated with the transcode operations, I know about those):
pImplement = DOMImplementationRegistry::getDOMImplementation("LS");
DOMDocument* pDoc = pImplement->createDocument("Examples", "example", NULL );
DOMElement* pRoot = pDoc->getDocumentElement();
DOMElement* firstElement = pDoc->createElementNS(("Examples", "example");
DOMElement* secondElement = pDoc->createElementNS("Examples", "example2");
DOMAttr* name = pDoc->createAttribute("Name");
XMLCh* somenameValue = XMLString::transcode("Fred");
name->setValue(somenameValue);
secondElement->setAttributeNode(name);
firstElement->appendChild(secondElement);
When I leave the method eventually, do I have to do anything special for firstElement, secondElement, name to free the memory from the createXXX methods? Or does pdoc own all the memory and I have to wait to destroy the DOMDocument?
If adds to the discussion, I loop over the name/value logic and add multiple attributes to the secondElement.
thanks.
From memory of my experience using DOM classes, you don't have to delete anything that you append to the DOM tree. For example, you append the child secondElement
to the element firstElement
. When the latter gets freed, it will also free secondElement
. However, I see two things here that will leak. First, you don't insert firstElement
into the DOM tree, and second, you don't explicitly delete pDoc
when you leave. You have to either free the element or add it to the DOM tree and delete it at some point latter in your code.
Note: by the name of the functions involved I'll assume you're talking about Xerces-C.
You only need to call release on the root of the tree (be it an DOMElement (to delete just a "branch' of the tree) or a DOMDocument (to delete the entire tree)).
So, adding a call to pDoc->release();
at the end will take care of freeing the memory of the document and all nodes attached to that document.
精彩评论