From e836264cdae59589a6052a61bd04d683705ad435 Mon Sep 17 00:00:00 2001
From: Alexander Golubev <fatzer2@gmail.com>
Date: Fri, 27 Mar 2026 20:16:22 +0300
Subject: kdesktop: fix a crash on rapid successful updates

In case there are several consequent updates of desktop (cause by e.g.
resolution changes), there could be a crash inside
slotFreeSpaceOverlayStart() cause by the fact that ItemViewItems stored
in m_paOutstandingFreeSpaceOverlays are getting deleted from the
KDIconView which invalidates the pointers. To avoid that, proactively
clean up m_paOutstandingFreeSpaceOverlays on items getting removed.

Realistically this can appear inside a virtual machine the resize of the
VM window results in several successive changes to resolution.

Signed-off-by: Alexander Golubev <fatzer2@gmail.com>
---
 kdesktop/kdiconview.cpp | 19 +++++++++++++++++++
 kdesktop/kdiconview.h   |  6 ++++++
 2 files changed, 25 insertions(+)

diff --git a/kdesktop/kdiconview.cpp b/kdesktop/kdiconview.cpp
index 8a20b59f1..26821f89f 100644
--- a/kdesktop/kdiconview.cpp
+++ b/kdesktop/kdiconview.cpp
@@ -1453,6 +1453,25 @@ void KDIconView::showFreeSpaceOverlay(KFileIVI* item)
     }
 }
 
+void KDIconView::takeItem( TQIconViewItem *item ) {
+    KFileIVI* kItem = static_cast<KFileIVI*>(item);
+
+    // Remove occurrences from items pending procession by slotFreeSpaceOverlayStart()
+    m_paOutstandingFreeSpaceOverlays.findRef(kItem);
+    while(m_paOutstandingFreeSpaceOverlays.current()) {
+        m_paOutstandingFreeSpaceOverlays.remove();
+        m_paOutstandingFreeSpaceOverlays.findNextRef(kItem);
+    }
+
+    KonqIconViewWidget::takeItem(item);
+}
+
+void KDIconView::clear() {
+    m_paOutstandingFreeSpaceOverlays.clear();
+
+    KonqIconViewWidget::clear();
+}
+
 void KDIconView::slotFreeSpaceOverlayStart()
 {
     do
diff --git a/kdesktop/kdiconview.h b/kdesktop/kdiconview.h
index 7ef8d4889..088106708 100644
--- a/kdesktop/kdiconview.h
+++ b/kdesktop/kdiconview.h
@@ -103,6 +103,12 @@ public:
     virtual void setWallpaper(const KURL &url) { emit newWallpaper( url ); }
     void setLastIconPosition( const TQPoint & );
 
+    /**
+     * Reimplemented from TQIconView to cleanup internal data (namely m_paOutstandingFreeSpaceOverlays)
+     */
+    virtual void takeItem( TQIconViewItem *item );
+    virtual void clear();
+
     static KURL desktopURL();
 
     /// KDirNotify interface, for trash:/
-- 
cgit v1.2.3

