QList 内部的 boost::shared_ptr 会导致分段错误
boost::shared_ptr inside QList leads to segmentation fault
我对QList和boost:shared_ptr有一个奇怪的问题。恐怕我无法将问题剥离开来,这就是为什么我稍后会发布wholoe函数的原因。
我想做什么:我有一个列表(_fileList),它将boost::shared ptrs存储到QFile对象。这些文件是 XML 文件。比我想解析这个文件并解决所有包含,这意味着将包含标签指定的文件也添加到_fileList并扫描它们以进一步包含标签。解析 3 个包含时,代码工作正常(在我的小测试中,每个文件只有一个包含)。第三次上线boost::shared_ptr 文件(*迭代);导致分段错误。如果你们中的任何人可以帮助我或给我提示如何找到此错误,我将很高兴。
void XmlParser::expandIncludes()
{
//search all files already in the file list for include tags, if any new are found append this file to the file list
QList<boost::shared_ptr<QFile> >::iterator iter = this->_fileList.begin();
while(iter!= this->_fileList.end())
{
boost::shared_ptr<QFile> file(*iter);
QDomDocument doc("activeFile");
if (!file->open(QIODevice::ReadOnly)){
return;
}
if (!doc.setContent(&(*file))) {
file->close();
return;
}
file->close();
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChildElement("include");
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
QString nextFile = e.text();
QString nextFileAbsolutePath = this->_workingDir.absolutePath() +"/"+nextFile;
boost::shared_ptr<QFile> newFileObject(new QFile(nextFileAbsolutePath));
this->_fileList.append(newFileObject);
}
n = n.nextSiblingElement("include");
}
doc.clear();
iter++;
}
}
在列表中插入新元素后,指向 QList 中元素的迭代器将变为无效。你可以改用QLinkList。
来自Qt容器文档:
只要 QLinkedList 中的某个项存在,指向该项的迭代器就保持有效,而指向 QList 的迭代器在任何插入或删除后都可能变为无效。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)