DOMDocument C++内存管理
关于 C++ 中 DOM* 类 createXXX 方法的问题。我是否需要执行任何特殊操作来释放从 DOM* createXXX 方法返回的内存?
例如(为了简化而删除了转码,以及与转码操作相关的变量的相关版本,我知道这些):
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);
当我最终离开该方法时,我是否必须对firstElement、secondElement、name进行任何特殊操作以释放createXXX 方法的内存?或者 pdoc 拥有所有内存,而我必须等待销毁 DOMDocument?
如果添加到讨论中,我会循环名称/值逻辑并向第二个元素添加多个属性。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我使用 DOM 类的经验,您不必删除附加到 DOM 树的任何内容。例如,您将子元素
secondElement
附加到元素firstElement
。当后者被释放时,它也会释放secondElement
。然而,我看到这里有两件事会泄漏。首先,您不会将firstElement
插入 DOM 树,其次,您离开时不会显式删除pDoc
。您必须释放该元素或将其添加到 DOM 树并在代码中稍后的某个时刻将其删除。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 elementfirstElement
. When the latter gets freed, it will also freesecondElement
. However, I see two things here that will leak. First, you don't insertfirstElement
into the DOM tree, and second, you don't explicitly deletepDoc
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.注意:根据所涉及的函数的名称,我假设您正在谈论 Xerces-C。
您只需要在根目录上调用 release树(无论是 DOMElement(仅删除树的“分支”)还是 DOMDocument(删除整个树)树))。
因此,在最后添加对
pDoc->release();
的调用将负责释放文档的内存以及附加到该文档的所有节点。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.