Search code examples
c++tinyxml

TinyXML throwing access violation


I've recently started using tinyXML. The problem is when I run my program to read through xml, it returns back an access violation. A common line is:

doc.FirstChildElement("Map")->FirstChildElement("Width")->GetText()

With the visual studio debugger, I determined that it is returning null for the 'Map', and then is using a null reference to call functions.

Here is the first lines of code, and the xml

XMLDocument doc;
    doc.LoadFile(path.c_str());

    int width = atoi(doc.FirstChildElement("Map")->FirstChildElement("Width")->GetText());

XML:

<?xml version="1.0"?>
 <Master>
 <Map>
    <Width>5</Width>
    <Height>5</Height>
    <Layers>1</Layers>
    <Tiles>
        <Tile>
            <Id>1</Id>
            <Path>data/tiles/dirt-base.png</Path>
        </Tile>
    </Tiles>
    <Data>
        <DataLayer>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</DataLayer>
    </Data>
 </Map>
 </Master>

IT is important to note, I originally did not have the root tag, but added it when it didn't work, adding it still didn't help though. Any help would be appreciated


Solution

  • Looks to me like one of those TinyXML functions you're calling is returning an invalid pointer. Try to check the result of each call separately and you'll be able to pin down the problem.