notNullGothik
notNullGothik

Reputation: 432

rapidXML, corrupted memory when traversing DOM tree

Don't understand what is going on with the attribute's memory and rapidXML. A function encapsulates the xml parsing, if success, returns a reference to the root node, when calling the traverse DOM tree inside this function I get the correct data stored in an xml file.

 typedef rapidxml::xml_node<>* Node;
 ...
 Node Load()
 {
     Node pRootNode = NULL;
     // read file stream in bytes
     ...
     std::vector<char> xmlCopy(bytes.begin(), bytes.end());
     xmlCopy.push_back('\0');
     rapidxml::xml_document<> doc;

    try
    {
      doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&bytes[0]);
      pRootNode = doc.first_node();
          ...
          TraverseDOMTree(pRootNode);
    }
    return pRootNode;
 }

TraverseDOMTree prints all attributes and node names as expected.

Later, obviously outside the scope of Load, pRootNode will be used to query values from the DOM three, this doesn't work. For testing purposes calling TraverseDOMTree, which perfectly worked, now prints attribute's garbage values. I can assume the DOM tree is still there, the same hierarchy of nodes as in the first call, but the attributes values are messed up. I tried making the rapidxml::xml_document<> doc global and also adding the parse_non_destructive flag, none of those make a difference.

If matters, the client using the Load method is running in the same thread. What can be wrong?

Upvotes: 2

Views: 924

Answers (1)

thiton
thiton

Reputation: 36049

std::vector<char> xmlCopy(bytes.begin(), bytes.end());

The local copy of the serial representation of your XML document is local. I would bet that rapidXML makes no copy of the attributes, but rather uses pointers to the sequence. You could check that by looking at the addresses of the attribute values and your document copy.

Upvotes: 3

Related Questions