一部の変数の値が重複しています。
ノードで初めてループするときはすべて正常に動作しますが、もう一度ループすると、最後に作成された変数の値が出力され、プログラムが動作を停止することがあります。
#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() から値を返す方法に問題があると思います)。