【发布时间】:2018-04-03 11:08:42
【问题描述】:
今天我试图从我的项目中找到内存泄漏,然后我遇到了下面的示例代码
std::string VersionValue("1.0");
std::string EncodingValue("UTF-8");
rapidxml::xml_document<> XMLDoc;
rapidxml::xml_node<> * pHeaderNode = XMLDoc.allocate_node(rapidxml::node_declaration);
pHeaderNode->append_attribute(XMLDoc.allocate_attribute("version", VersionValue.c_str()));
pHeaderNode->append_attribute(XMLDoc.allocate_attribute("encoding", EncodingValue.c_str()));
我打开了 rapidxml 代码,在 allocate_attribute() 里面我看到了它的分配内存
xml_attribute<Ch> *attribute = new(memory) xml_attribute<Ch>;
在append_attribute() 内部,它把内存分配给它的成员变量。
没有为xml_document 声明析构函数。那么它是如何删除属性的呢? valgrind 从上面的示例代码中返回 0 个内存泄漏。怎么可能?
【问题讨论】:
-
@Joe new 必须在某个时候被删除,对吧?
-
看起来是新的展示位置
-
new(memory)没有分配任何东西,而是使用*memory已经存在的内存。 -
谢谢@Alexander 我不知道安置新