Iterate over a const copy list of containers

This effectively reverts the mutable iterations approach on
2448f88c5f, and fix crashes
and ghost items when using the filter bar

BUG: 428374
This commit is contained in:
Ismael Asensio 2020-11-06 21:22:14 +01:00 committed by Elvis Angelaccio
parent e270aae920
commit ee4ab8ce69
3 changed files with 10 additions and 10 deletions

View file

@ -919,9 +919,9 @@ void KFileItemModel::slotCompleted()
// Note that the parent folder must be expanded before any of its subfolders become visible.
// Therefore, some URLs in m_restoredExpandedUrls might not be visible yet
// -> we expand the first visible URL we find in m_restoredExpandedUrls.
QMutableSetIterator<QUrl> it(m_urlsToExpand);
while (it.hasNext()) {
const QUrl url = it.next();
// Iterate over a const copy because items are deleted and inserted within the loop
const auto urlsToExpand = m_urlsToExpand;
for(const QUrl &url : urlsToExpand) {
const int indexForUrl = index(url);
if (indexForUrl >= 0) {
m_urlsToExpand.remove(url);

View file

@ -957,9 +957,9 @@ void KFileItemModelRolesUpdater::updateChangedItems()
QList<int> visibleChangedIndexes;
QList<int> invisibleChangedIndexes;
QMutableSetIterator<KFileItem> it(m_changedItems);
while (it.hasNext()) {
const KFileItem item = it.next();
// Iterate over a const copy because items are deleted within the loop
const auto changedItems = m_changedItems;
for (const KFileItem item : changedItems) {
const int index = m_model->index(item);
if (index < 0) {

View file

@ -1155,10 +1155,10 @@ void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
QVector<int> itemsToMove;
// Remove all KItemListWidget instances that got deleted
QMutableHashIterator<int, KItemListWidget*> it(m_visibleItems);
while (it.hasNext()) {
it.next();
KItemListWidget* widget = it.value();
// Iterate over a const copy because the container is mutated within the loop
// directly and in `recycleWidget()` (https://bugs.kde.org/show_bug.cgi?id=428374)
const auto visibleItems = m_visibleItems;
for (KItemListWidget* widget : visibleItems) {
const int i = widget->index();
if (i < firstRemovedIndex) {
continue;