QList と boost:shared_ptr に奇妙な問題があります。私は問題を切り離すことができないのではないかと心配しています。そのため、後でwholoe関数を投稿します。
やりたいこと: boost::shared ptrs を QFile オブジェクトに格納するリスト (_fileList) があります。ファイルは XML ファイルです。このファイルを解析してすべてのインクルードを解決したいのですが、これは、インクルード タグで指定されたファイルを _fileList にも追加し、さらにインクルード タグをスキャンすることを意味します。3 つのインクルードを解決すると、コードは正常に動作します (私の小さなテストでは、ファイルごとに 1 つのインクルードしかありません)。3 回目の行 boost::shared_ptr file(*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++;
}
}