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.
-
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++; } }
-
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
-
Bug report. : https://bugreports.qt.io/browse/QTBUG-61094
-
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.