在构建时工作,但在调试器中有问题

RapidXml Work when build but have problems in debugger

本文关键字:调试器 有问题 构建 工作      更新时间:2023-10-16

嗨,我使用rapidxml在我的游戏中加载地图,这就是我的加载类的样子它正常编译,但加载文件时,有时它崩溃,所以我想调试它,但调试器不喜欢我的功能,这是设置指针到XML文件中的数据。

#0 0042D5A6 rapidxml::xml_node<char>::first_node(this=0x0, name=0x484175 <_ZSt16__convert_from_vRKPiPciPKcz+4735349> "MapInfo", name_size=7, case_sensitive=true) (C:/.../rapidxml.hpp:942)
#1 00404E31 MapLoader::SetNodes(this=0x27fc1c) (C:...main.cpp:651)
#2 004032F6 main() (C:...main.cpp:267)

class MapLoader
{
public:
xml_document<> doc;
file<>xmlFile(char);
string ca,cb,cc,cd;
xml_node<> *test;
xml_node<> *root;
    xml_node<> *mapinfo;
        xml_node<> *name;
        xml_node<> *date;
        xml_node<> *msize;
            xml_attribute<> *sizex;
            xml_attribute<> *sizey;
    xml_node<> *mapdata;
        xml_node<> *layer;
            xml_attribute<> *nr;
                xml_node<> *tile;
                    xml_attribute<> *id;
                    xml_attribute<> *x;
                    xml_attribute<> *y;
void LoadFile(const char *filename);
void SetNodes();
void FillVector();
void SaveVector();
};
void MapLoader::SetNodes()
{
    root=doc.first_node("root");
        mapinfo=root->first_node("MapInfo");   //////debugger is pointing on this line
            name=mapinfo->first_node("Name");
            date=mapinfo->first_node("Date");
            msize=mapinfo->first_node("Size");
                sizex=msize->first_attribute("x");
                sizey=msize->first_attribute("y");
        mapdata=root->first_node("MapData");
            layer=mapdata->first_node("Layer");
                nr=layer->first_attribute("id");
                    tile=layer->first_node("Tile");
                        id=tile->first_attribute("id");
                        x=tile->first_attribute("x");
                        y=tile->first_attribute("y");
}

我能做些什么来修理它或类似的事情?

编辑:下面是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <MapInfo>
        <Name>Test</Name>
        <Date>17.08.2014</Date>
        <Size x="64" y="64"/>
    </MapInfo>
    <MapData>
        <Layer nr="1">
            <Tile id="1" x="32" y="32"/>
            <Tile id="1" x="32" y="64"/>
            <Tile id="1" x="512" y="64"/>
        </Layer>
        <Layer nr="2"/>
        <Layer nr="3"/>
    </MapData>
</root>

首先,检查返回的指针是否为NULL是一个好习惯。我的猜测是,"root"在xml文件中缺失。

看一下rapidxml手册,因为大小写敏感也可能是一个问题。

编辑:

我写了一个小程序,至少检查了NameDate

// Trying out rapid xml.
//
#include "rapidxml/rapidxml.hpp"
#include <cstdio>
#include <string>
#include <vector>
int main(int argc, char** argv) {
    rapidxml::xml_document<> doc;    // character type defaults to char
    FILE* file = fopen("Test.xml", "r");
    if (!file)
        return 1;
    // file exists.
    // get the number of bytes.
    fseek(file, 0, SEEK_END);
    size_t sizeInBytes = ftell(file);
    fseek(file, 0, SEEK_SET);
    char* buffer = static_cast<char*>(malloc(sizeInBytes + 1));  // + 1 needed?
    if (fread(buffer, 1LU, sizeInBytes, file) != sizeInBytes) {
        perror("unexpected file lengthn");
        fclose(file);
        free(buffer);
        return 1;
    }
    buffer[sizeInBytes] = 0;
    // close the file.
    fclose(file);
    doc.parse<0>(buffer);        // 0 means default parse flags
    std::vector<std::string> nodeNames;
    nodeNames.push_back("Name");
    nodeNames.push_back("Date");
    rapidxml::xml_node<>* node = doc.first_node("root");
    for (size_t i = 0; i < nodeNames.size() && node; ++i) {
        if (node->first_node(nodeNames[i].c_str()))
            fprintf(stderr, "CantFindNode with name %s.n", nodeNames[i].c_str());
        printf("Node %s found!n", nodeNames[i].c_str());
    }
    free(buffer);
    return 0;
  }