DOMDocument C++内存管理

发布于 2024-11-01 18:09:57 字数 911 浏览 6 评论 0原文

关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-11-08 18:09:57

根据我使用 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 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.

我喜欢麦丽素 2024-11-08 18:09:57

注意:根据所涉及的函数的名称,我假设您正在谈论 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文