Menu

Access violation after reading data

Phil
2005-08-01
2013-05-20
  • Phil

    Phil - 2005-08-01

    HI,

    i am using the c++ builder to read data from a xml file. i have build me a little example and i can read between two tags. problem is only that i get an access violation, after finishing.

    i am using this little xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <product>
    <a001>A30</a001>
    <a002>03</a002>
    </product>

    and here my function to read the data:

    bool __fastcall ParseXML()
    {  
       TiXmlDocument doc("c:\\test.xml");
       doc.LoadFile();
       TiXmlHandle handle(&doc);
       TiXmlText *result;

       // save the data here
       AnsiString uniqueid = "";

       // get the data
       result = handle.ChildElement("product", 0).ChildElement("a001", 0).FirstChild().Text();
       if (result)
       {
          uniqueid = result->Value();
       }

       ShowMessage(uniqueid);

       delete result;
       return true;
    }

    ansistring is a c++ builder string type. everythings works fine, but after the ShowMessage function, i get this access violation.

    my compiler switch to tinyxml.cpp to line 646. this line contains Clear(); It is in the

    void TiXmlElement::ClearThis() function.

    Could anybody tell me why i get this violation. Is there anything missing in my code?

    Phil

     
    • Ellers

      Ellers - 2005-08-01

      I'm fairly sure that you don't own the result variable above, so deleting it is a mistake.

      Text() returns a pointer to the internal string within the node. Therefore, when the document goes out of scope, and the tree cleans itself up, it naturally deletes the data it owns.

      At least, I'm fairly sure about this...

      HTH
      Ellers

       
    • Phil

      Phil - 2005-08-01

      hi ellers,

      this was the problem. when i comment out

      delete result;

      it works fine. i dont know why i use a delete, it was wont :)

      Phil

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.