在使用TinyXml for C ++时,我需要清理哪些内存管理?

时间:2022-11-09 19:52:30

I'm doing the following with TinyXml:

我正在使用TinyXml执行以下操作:

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
TiXmlElement* main = new TiXmlElement("main");

TiXmlElement* header = new TiXmlElement("header");
header->SetAttribute("attribute","somevalue");
main->LinkEndChild(header);

// ... Add many more TiXmlElment* to other elements all within "main" element

doc.LinkEndChild(decl);
doc.LinkEndChild(main);

// ... do stuff with doc

// Now I am done with my doc. What memory management happens here? 

At the end of my program's execution, will all of the TiXmlElement* be cleaned up when the doc goes out of scope? Do I need to walk the doc tree and free up all of the memory myself?

在程序执行结束时,当文档超出范围时,是否会清除所有TiXmlElement *?我是否需要遍历文档树并自行释放所有内存?

2 个解决方案

#1


The documentation for LinkEndChild says this:

LinkEndChild的文档说明了这一点:

NOTE: the node to be added is passed by pointer, and will be henceforth owned (and deleted) by tinyXml. This method is efficient and avoids an extra copy, but should be used with care as it uses a different memory model than the other insert functions.

注意:要添加的节点由指针传递,然后由tinyXml拥有(并删除)。此方法是有效的并且避免了额外的副本,但是应该小心使用,因为它使用与其他插入函数不同的内存模型。

#2


Anything you allocate with new will never be automatically cleaned up -- you (or at least, someone) needs to call delete header; etc.

你用new分配的任何内容都不会被自动清理 - 你(或至少某人)需要调用delete header;等等

I say "someone" because it's possible that the TiXmlDocument object takes ownership of these objects and cleans them up itself -- the only way to know that is to check TinyXML's documentation.

我说“有人”,因为TiXmlDocument对象可能拥有这些对象的所有权并自行清理它们 - 知道这一点的唯一方法是检查TinyXML的文档。

If it doesn't take ownership, you're better off just declaring local objects on the stack:

如果它不占用所有权,那么最好只在堆栈上声明本地对象:

TiXmlDeclaration decl( "1.0", "", "" );    // etc.

If you need the objects to persist past the end of the function, it's safer to use shared pointers, e.g. Boost's shared_ptr.

如果您需要将对象保留在函数末尾,则使用共享指针会更安全,例如Boost的shared_ptr。

#1


The documentation for LinkEndChild says this:

LinkEndChild的文档说明了这一点:

NOTE: the node to be added is passed by pointer, and will be henceforth owned (and deleted) by tinyXml. This method is efficient and avoids an extra copy, but should be used with care as it uses a different memory model than the other insert functions.

注意:要添加的节点由指针传递,然后由tinyXml拥有(并删除)。此方法是有效的并且避免了额外的副本,但是应该小心使用,因为它使用与其他插入函数不同的内存模型。

#2


Anything you allocate with new will never be automatically cleaned up -- you (or at least, someone) needs to call delete header; etc.

你用new分配的任何内容都不会被自动清理 - 你(或至少某人)需要调用delete header;等等

I say "someone" because it's possible that the TiXmlDocument object takes ownership of these objects and cleans them up itself -- the only way to know that is to check TinyXML's documentation.

我说“有人”,因为TiXmlDocument对象可能拥有这些对象的所有权并自行清理它们 - 知道这一点的唯一方法是检查TinyXML的文档。

If it doesn't take ownership, you're better off just declaring local objects on the stack:

如果它不占用所有权,那么最好只在堆栈上声明本地对象:

TiXmlDeclaration decl( "1.0", "", "" );    // etc.

If you need the objects to persist past the end of the function, it's safer to use shared pointers, e.g. Boost's shared_ptr.

如果您需要将对象保留在函数末尾,则使用共享指针会更安全,例如Boost的shared_ptr。