某些变量中的值正在重叠。
当我第一次在节点上循环时,一切正常,但是当我再次循环时,会打印最后一个创建的变量的值,有时程序会停止工作。
#include "pugixml.hpp"
#include "pugixml.cpp"
pugi::xml_node varnodes = getNodesXML(varsFilepath);
for (pugi::xml_node node = varnodes.first_child(); node; node = node.next_sibling()){
printf("%s: %s\n", node.name(), node.attribute("id").value());
}
pugi::xml_node blocknodes = getNodesXML(blocksFile);
for (pugi::xml_node node = blocknodes.first_child(); node; node = node.next_sibling()){
printf("%s: %s\n", node.name(), node.attribute("id").value());
//varnodes.append_copy(node);
}
pugi::xml_node funcnodes = getNodesXML(functionsFile);
for (pugi::xml_node node = funcnodes.first_child(); node; node = node.next_sibling()){
printf("%s: %s\n", node.name(), node.attribute("id").value());
//varnodes.append_copy(node);
}
//looping on varnodes after other nodes have been created (the program crash and this is not displayed)
for (pugi::xml_node node = varnodes.first_child(); node; node = node.next_sibling())
printf("%s: %s\n", node.name(), node.attribute("id").value());
for (pugi::xml_node node = blocknodes.first_child(); node; node = node.next_sibling())
printf("%s: %s\n", node.name(), node.attribute("id").value());
for (pugi::xml_node node = funcnodes.first_child(); node; node = node.next_sibling())
printf("%s: %s\n", node.name(), node.attribute("id").value());
这就是我从不同文件中获取节点的方式:
pugi::xml_node getNodesXML(char *filepath){
printf("%s\n",filepath);
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filepath);
if(!result) printf("Error reading file: %s\n%s\n", filepath, result.description());
pugi::xml_node nodes = doc.child("nodes");
if(!nodes) printf("Error finding root <nodes> in: \n%s\n", filepath);
return nodes;
}
xml 是这样的:
varnodes.xml <nodes><node id="firstvar"></node></nodes>
blocknodes.xml <nodes><node id="firstblock"></node></nodes>
funcnodes.xml <nodes><node id="firstfunc"></node></nodes>
//Expected output:
node: firstvar
node: firstblock
node: firstfunc
node: firstvar
node: firstblock
node: firstfunc
//Wrong output Im getting (sometimes the program just stops working):
node: firstvar
node: firstblock
node: firstfunc
node: firstfunc
node: firstfunc
node: firstfunc
错误:practice.exe 中 0x00eb0cdd 处的未处理异常:0xC0000005:访问冲突读取位置 0xfeeeff0a。
main.exe 已停止工作并将我指向此功能:
PUGI__FN xml_attribute xml_node::attribute(const char_t* name_) const
{
if (!_root) return xml_attribute();
> for (xml_attribute_struct* i = _root->first_attribute; i; i = i->next_attribute)
if (i->name && impl::strequal(name_, i->name))
return xml_attribute(i);
return xml_attribute();
}
当我将函数中的代码添加到 main.cpp 时,变量值打印完美(我认为我从 getNodesXML() 返回值的方式有问题)