Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QUrlList crash using iterator.



  • I have a treeview on one window with a QFileSystemModel and dragging from this to another window crashes with the following code

    // This crashes
        if (event->mimeData()->hasUrls()) {
            QList<QUrl>::iterator i = event->mimeData()->urls().begin();
            while (i != event->mimeData()->urls().end()) {
                qDebug() << (*i).path()<< endl;
                i++;
            }
        }
        // This does not
        if (event->mimeData()->hasUrls()) {
            QList<QUrl> urlList = event->mimeData()->urls();
            for (int i = 0; i < urlList.size(); ++i) {
                qDebug() << urlList.at(i).path()<< endl;
            }
        }
    }
    

    I've uploaded a demo project here.

    https://github.com/conradjones/qt-list-crash



  • @conradjones

    Both code sections do not do exactly the same thing for lists longer than 32 entries. Is there already the reason for your crash?

    Personally, I would hold the list for the first example seaparetely anyhow. There you can check the size of teh list and add a counter in your while loop.



  • @koahnig I've updated the example.

    You are however correct about holding the list locally. This works. Interesting, I was trying to avoid copying constructing the whole list. I still see this as a bug.

        if (event->mimeData()->hasUrls()) {
            QList<QUrl> urlList = event->mimeData()->urls();
            QList<QUrl>::iterator i = urlList.begin();
            while (i != urlList.end()) {
                qDebug() << (*i).path()<< endl;
                i++;
            }
        }
    


  • @conradjones

    It looks like a bug, assuming that the list may not be changed during your while loop either through event or mimedata. However, you are the only one who can know that part.

    Bugs need to be reported to JIRA



  • @koahnig It probably is being changed and the iterator becoming invalidated. Not by me by QT, all I'm doing is handling the drag enter event. I haven't got any threads happening.




  • Lifetime Qt Champion

    There is no bug, to sum up the comment on the bug report: in your crashing case you are working with different lists on each iteration.


Log in to reply