昨天我花了一些时间调试一个可爱的Heisenbug,它的xmlNodePtr
的类型会改变。此示例显示错误:
#include <iostream>
#include <vector>
#include <string>
#include <memory> // std::unique_ptr
#include <cstdint>
#include <libxml/tree.h>
#include <libxml/parser.h>
struct SomeDataType {
std::vector<std::vector<std::string>> data;
explicit SomeDataType(uint32_t rows_, uint32_t columns_)
: data(rows_)
{
for (uint32_t row = 0; row < rows_; ++row) {
data[row].resize(columns_);
}
}
};
static std::vector<xmlNodePtr> GetChildren(xmlNodePtr node)
{
std::vector<xmlNodePtr> children;
xmlNodePtr child = node->children;
while (child) {
if (child->type == XML_ELEMENT_NODE) {
children.push_back(child);
}
child = child->next;
}
return children;
}
int main() {
std::unique_ptr<xmlDoc, void(*)(xmlDoc*)> document = { xmlParseEntity("libxml2-fail.xml"), xmlFreeDoc };
SomeDataType{ 3, 2 };
xmlNodePtr root = xmlDocGetRootElement(document.get());
for (const xmlNodePtr &child : GetChildren(root)) {
const xmlNodePtr &entry = GetChildren(child)[0]; // Problem here...
std::cout << "Expected " << XML_ELEMENT_NODE << " but was " << entry->type << std::endl;
std::cout << entry->name << std::endl;
}
}
编译:
g++ -g -std=c++14 -Wall -Wextra -pedantic -I/usr/include/libxml2 libxml2-fail.cpp -lxml2 -o fail.out
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<data>
<tag>
<subtag>1</subtag>
</tag>
</data>
运行给我以下输出:
Expected 1 but was 17
逐步执行gdb,直到到达
const xmlNodePtr & = ...
行,一切都很好。而不是XML_ELEMENT_NODE
类型,而是XML_ENTITY_DECL
类型。但是,如果我运行以下命令,则引用xmlNodePtr
会变成我期望的类型:48 const xmlNodePtr &entry = GetChildren(child)[0];
(gdb) n
49 std::cout << "Expected " << XML_ELEMENT_NODE << " but was " << entry->type << std::endl;
(gdb) p *entry
$1 = {_private = 0x0, type = XML_ENTITY_DECL, name = 0x0, children = 0xb7e67d7c <std::string::_Rep::_S_empty_rep_storage+12>, last = 0x0, parent = 0x69, next = 0x0, prev = 0x9, doc = 0x0, ns = 0x805edb8, content = 0x805edb8 "", properties = 0x0, nsDef = 0x0, psvi = 0x0, line = 60648, extra = 2053}
(gdb) p *child
$2 = {_private = 0x0, type = XML_ELEMENT_NODE, name = 0x805ee98 "tag", children = 0x805eea8, last = 0x805ef98, parent = 0x805edb8, next = 0x805efe8, prev = 0x805ee08, doc = 0x805ece8, ns = 0x0, content = 0x0, properties = 0x0, nsDef = 0x0, psvi = 0x0, line = 3, extra = 0}
(gdb) p GetChildren(child)
$3 = std::vector of length 1, capacity 1 = {0x805eef8}
(gdb) p *entry
$4 = {_private = 0x0, type = XML_ELEMENT_NODE, name = 0x805ef38 "subtag", children = 0x805ef48, last = 0x805ef48, parent = 0x805ee58, next = 0x805ef98, prev = 0x805eea8, doc = 0x805ece8, ns = 0x0, content = 0x0, properties = 0x0, nsDef = 0x0, psvi = 0x0, line = 4, extra = 0}
(gdb)
当我像这样遍历一个元素时,我没有问题:
for (const xmlNodePtr &entry : GetChildren(child)) {
...
}
当我不像这样将
xmlNodePtr
设为const引用时,我也没有问题:xmlNodePtr entry = GetChildren(child)[0];
但是,根据this stackoverflow question,这应该不是问题。
奇怪的是
SomeDataType
结构是必需的;否则我会遇到段错误,因为entry
变为空指针。此错误来自何处?
最佳答案
执行此操作时:
const xmlNodePtr &entry = GetChildren(child)[0]; // Problem here...
您正在以一种不会延长生命周期的方式有效地将引用绑定(bind)到临时目录。
operator[]
返回一个引用,因此您没有将引用绑定(bind)到临时对象,而是将引用绑定(bind)到引用。但是从operator[]
返回的引用引用了vector
返回的底层临时GetChildren()
中的一个元素,该元素超出了行尾的范围,使您自己悬空了。但是,当您尝试尝试以下操作时:
for (const xmlNodePtr &entry : GetChildren(child)) {
这是语法糖:
{
auto&& __range = GetChildren(child); // bind temporary to reference
// lifetime IS extended
auto b = begin(__range);
auto e = end(__range);
for (; b != e; ++b) {
const xmlNodePtr& entry = *b;
// ...
}
}
在这里,
*b
不是临时的,也不是临时的任何部分-它是对容器的引用,该容器的生存期与__range
一样长,这是整个循环的过程。无悬空引用。同样,
xmlNodePtr entry = GetChildren(child)[0];
只是复制,没有任何引用问题。