The value in some variables are being overlapped.
When I loop the first time on a node everything works fine but when I loop again, the values from the last variable created are printed and sometimes the program stops working.
#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());
This is how I get the nodes from different files:
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;
}
The xml is something like this:
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
Error:
Unhandled exception at 0x00eb0cdd in practice.exe: 0xC0000005: Access violation reading location 0xfeeeff0a.
main.exe has stopped working and points me to this function:
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();
}
When I add the code from the function to main.cpp the variables values print perfectly (I think there is something wrong in the way I return a value from getNodesXML() )
I think I solved it:
In the function I create an object of type pugi::xml_document which is local, if I create the doc object as a pointer everything works correctly but I guess I'll have a memory leak so probably I need another function that returns that object to delete later or create a global variable to store it.