1

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

1 に答える 1

2

リストに新しい要素を挿入すると、QList 内の要素を指す反復子が無効になります。代わりに QLinkList を使用できます。

Qtコンテナのドキュメントから:

QLinkedList 内の項目を指す反復子は、項目が存在する限り有効なままですが、QList への反復子は、挿入または削除後に無効になる可能性があります。

于 2012-08-15T00:48:26.320 に答える