【发布时间】:2016-05-24 16:45:01
【问题描述】:
所以我最近掌握了 RapidXML 以用作在我的程序中解析 XML 的一种方式,我主要是用它来解决问题,但我遇到了一些非常奇怪的问题,我真的很苦恼追查。试着坚持下去,因为我非常彻底地试图解决这个问题,但我一定错过了一些东西。
首先是 XML:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<image key="tilemap_roguelikesheet" path="res/media/tilemaps/roguelikesheet.png" />
<image key="tilemap_tiles" path="res/media/tilemaps/tiles.png" />
</resources>
segfault发生的函数:
void TextureManager::LoadResource(const char* pathToFile)
{
rapidxml::xml_document<>* resource = Resources::LoadResource(pathToFile);
std::string imgName;
std::string imgPath;
if (resource != NULL)
{
rapidxml::xml_node<>* resourcesNode = resource->first_node("resources");
if (resourcesNode != NULL)
{
for (rapidxml::xml_node<>* child = resourcesNode->first_node("image"); child; child = child->next_sibling())
{
//Crash here on the second loop through.
imgName = child->first_attribute("key")->value();
imgPath = child->first_attribute("path")->value();
Astraeus::Log(moduleName, "Image Name: " + imgName);
Astraeus::Log(moduleName, "Image Path: " + imgPath);
TextureManager::AddTexture(imgName, imgPath);
}
}
else
{
Astraeus::Error(moduleName, "Resources node failed to load!");
}
resource->clear();
}
else
{
std::string fileName(pathToFile);
Astraeus::Error(moduleName, fileName + " could not be loaded.");
}
}
因此,segfault 发生在 for 循环的第二个循环中,以遍历所有节点,并在它尝试执行 imgName 分配时触发。这就是事情变得有点奇怪的地方。在对程序进行调试时,初始子节点故障显示它具有指向下一个节点的内存指针以及它的元素/属性等。在调查这些节点时,您可以看到这些值存在并且 rapidxml 似乎已成功解析文件。
但是,当第二个循环发生时,child 显示仍然具有完全相同的内存指针,但这次值的细分显示它们本质上是 NULL 值,因此程序失败,我们得到代码 139。如果你试试看前面的节点,我们刚过来的那个值也是NULL。
现在说,我注释掉调用 AddTexture 函数的行,节点能够打印出所有节点值,一点问题都没有。 (Log 方法本质上只是打印到控制台,直到我用它做一些更时髦的东西。)所以问题一定出在函数上?这里是:
void TextureManager::AddTexture(const std::string name, const std::string path)
{
Astraeus::Log(moduleName, "Loading texture: " + path);
if (texturesLookup.find(name) != texturesLookup.end())
{
Astraeus::Error(moduleName, "Texture Key: " + name + " already exists in map!");
}
else
{
texturesLookup.insert(std::make_pair(name, path));
//Texture* texture = new Texture();
/*if (texture->LoadFromFile(path))
{
//textures.insert(std::make_pair(name, texture));
}
else
{
Astraeus::Error(moduleName, "Failed to add texture " + name + " to TextureManager!");
}*/
}
}
忽略字符串传递的事实,因此不应该以任何方式影响节点,这个函数仍然有点不确定。如果我注释掉它可以工作的所有内容,但有时会再次崩溃。一些代码被注释掉了,因为我没有直接添加键名,加上指向纹理的内存指针,而是切换到存储键和路径字符串,然后我可以稍后将纹理加载到内存中作为解决方法。这个解决方案工作了一点,但果然又开始出现段错误。
我无法真正可靠地复制或缩小每次导致问题的原因,因此希望能提供任何帮助。 RapidXML 文档是否以某种方式超出范围或被删除?
为了记录,该类与存储纹理指针的地图实际上只是静态的。
谢谢!
【问题讨论】:
-
我不熟悉您正在使用的 API,但请注意,我知道的大多数 XML API 中的下一个兄弟可以返回任何类型的节点,而不仅仅是元素节点,因此您可能会点击文本两个元素之间的节点。
-
RapidXML 的执行方式略有不同,因为 next_sibling() 确实获得了下一个正确的节点(在本例中为
),而不是将 节点的内容视为自己的文本节点。如果您希望从中获取文本,则必须在节点上调用 value() (在我的子代码中)。我还调试了节点值,所以我知道 next_sibling() 将调用并且实际上可以看到下一个正确的节点,除非它循环时,所有这些值都无效,直到查看指向前一个节点的指针,显示它的值为空。 -
您的
image元素为空,但image元素之间只有空白文本节点。如果您有,您的代码是否有效?<resources><image key="tilemap_roguelikesheet" path="res/media/tilemaps/roguelikesheet.png" /><image key="tilemap_tiles" path="res/media/tilemaps/tiles.png" /></resources>? -
不,问题仍然存在。