2012-08-15 70 views
1

我有一个QList和boost:shared_ptr奇怪的问题。恐怕我无法分开解决问题,这就是为什么我稍后会发布wholoe功能。boost :: shared_ptr里面QList导致分段错误

我想做的事情:我有一个列表(_fileList),它将存储boost :: shared ptrs存储到QFile对象。这些文件是XML文件。比我想解析这个文件并解决所有包含这意味着将包含标记指定的文件也添加到_fileList并扫描它们以获取更多包含标记。代码工作正常时,解决3包括(在我的小测试中只有一个包括每个文件)。第三次行 boost :: shared_ptr文件(* iter);导致分段错误。 如果你们中的任何一位能帮助我或给我提示我如何找到这个错误,我会很高兴。您插入新元素到列表中后

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++; 
    } 
} 

回答

2

迭代器指向一个元素的QList变得无效。您可以改为使用QLinkList。

与qt Container文档:

迭代器指向在QLinkedList一个项目保持只要项目中存在有效的,而迭代到的QList可以成为任何插入或移除后无效。

+0

谢谢你解决我的问题。 – Thorsten 2012-08-15 00:57:16