2012-08-15 72 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