From 3e6724b11a53a022f9f6547a2201a98fef535f50 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 10 Aug 2023 19:26:02 +0300 Subject: [PATCH] QAbstractItemView: port Q_FOREACH to ranged-for Take a copy as the loop may end up calling QCoreApplication::postEvent() which in turn could invoke unknown code that results in modifying the QHash that is being iterated over. Task-number: QTBUG-115803 Change-Id: I5f8aabb13ef7f60b470d0d3a986063775e787036 Reviewed-by: Richard Moe Gustavsen --- src/widgets/itemviews/qabstractitemview.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index 7087b98726..41aba6297c 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -1121,7 +1121,11 @@ void QAbstractItemView::reset() { Q_D(QAbstractItemView); d->delayedReset.stop(); //make sure we stop the timer - foreach (const QEditorInfo &info, d->indexEditorHash) { + // Taking a copy because releaseEditor() eventurally calls deleteLater() on the + // editor, which calls QCoreApplication::postEvent(), the latter may invoke unknown + // code that may modify d->indexEditorHash. + const auto copy = d->indexEditorHash; + for (const auto &[index, info] : copy.asKeyValueRange()) { if (info.widget) d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data())); }