简体   繁体   中英

Segmentation fault when using rapidxml::xml_node<char>::first_attribute

I'm learning to use rapidXML, but when I try to read big XML file (I posted it on pastebin, because it more than 400 lines long) I get Segmentation fault on line 78 when it loops 4th time, so I thing it is something like small buffer, but I have no idea what to do. my code:

#include <rapidxml/rapidxml.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <glm/glm.hpp>
#include <algorithm>
#include <vector>

using namespace rapidxml;

int main(){
    std::ifstream fule("./xml.xml");
    if(!fule){
        std::cout << "failed to open file";
    }
    std::string token, word;//to count number of layers
    word = "</layer>";
    int noLayers = 0;
    while(fule>>token){
        if(word==token){
            noLayers++;
        }
    }
    std::cout << std::endl << noLayers << std::endl;
    fule.close();
    std::ifstream file("./xml.xml");
    if(!file){
        std::cout << "failed to open file";
    }
    std::stringstream buffer;
    buffer << file.rdbuf();
    xml_document<> doc;
    std::string content(buffer.str());
    doc.parse<0>(&content[0]);
    file.close();


    xml_node<> *pRoot = doc.first_node();//<in this case <map> node, get root node
    std::string gameVersion, orientation, renderOrder; //used later
    int mapWidth, mapHeight, tileWidth, tileHeight, playerStartPositionX, playerStartPositionY;
   
    xml_attribute<> *pAttr = pRoot->first_attribute(); 
    pAttr = pRoot->first_attribute("version");
    gameVersion = pAttr->value();
    pAttr = pRoot->first_attribute("orientation");
    orientation = pAttr->value();
    pAttr = pRoot->first_attribute("renderorder");
    renderOrder = pRoot->value();

    pAttr = pRoot->first_attribute("width");
    mapWidth = atoi(pAttr->value());
    pAttr = pRoot->first_attribute("height");
    mapHeight = atoi(pAttr->value()); 
    pAttr = pRoot->first_attribute("tilewidth");
    tileWidth = atoi(pAttr->value());
    pAttr = pRoot->first_attribute("tileheight");
    tileHeight = atoi(pAttr->value());
    xml_node<> *pNode = pRoot->first_node("properties");
    for(xml_node<> *propNode = pNode->first_node("property"); propNode; propNode = propNode->next_sibling()){
        pAttr = propNode->first_attribute("name");
        std::string s = pAttr->value();
        if(s == "PlayerStartPositionX"){
            pAttr = propNode->first_attribute("value");
            playerStartPositionX = atoi(pAttr->value());
        }if(s == "PlayerStartPositionY"){
            pAttr = propNode->first_attribute("value");
            playerStartPositionY = atoi(pAttr->value());
        }
    }
   
    int i= 0;//to count loops of next for loop 
    int m_levelData[mapWidth][mapHeight][noLayers];//make it [sizeX][sizeY][numOfLayers]
    for(pNode = pRoot->first_node("layer"); pNode; pNode = pNode->next_sibling()){
    //TODO: get layer info <++> 
    xml_node<> *dataNode = pNode->first_node("data");dataNode;
    pAttr = dataNode->first_attribute("encoding");
    std::string s = pAttr->value();
    std::vector<std::string> levelData;
    std::string tmp;
    if(s == "csv"){
        s = dataNode->value();
        std::replace(s.begin(), s.end(), ',', ' ');
        std::stringstream ss(s);
        while(std::getline(ss, tmp)){
            if(!tmp.empty()){
            levelData.push_back(tmp);
            }
        }
            for(int j = 0; j < /*sizeY*/ 100; j++){
                std::stringstream in(levelData[j]);
                std::vector<int> v;
                int temp;
                while(in >> temp){
                    v.push_back(temp);
                } 
                for(int k = 0; k < /*sizex*/ 100;k++){
                   m_levelData[k][j][i] = v[k]; 
                }
            }
    } else {
        std::cout << "*AUTISTIC SCREAMING*" << std::endl;
    }
    i++;
    }
}

(this is only temporary code to figure out how rapidXML works)

Edit:

The XML file was generated by Tiled map editor

It appears that your XML is malformed. When I removed the new lines after the opening data tag and before the closing tag, the program executed as I would expect. No errors were thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM