From 562e6bb3efa7feea31cd066e341315c27bf94720 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 25 Aug 2020 13:07:47 +0200 Subject: [PATCH] Copy listeners before notifying. As raising the event can cause the listeners to change. --- .../Utils/CollectionChangedEventManager.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls/Utils/CollectionChangedEventManager.cs b/src/Avalonia.Controls/Utils/CollectionChangedEventManager.cs index 6abba0cc8e..fa3f684747 100644 --- a/src/Avalonia.Controls/Utils/CollectionChangedEventManager.cs +++ b/src/Avalonia.Controls/Utils/CollectionChangedEventManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.Linq; using System.Runtime.CompilerServices; using Avalonia.Threading; using Avalonia.Utilities; @@ -118,16 +119,17 @@ namespace Avalonia.Controls.Utils if (sender is INotifyCollectionChanged incc && _entries.TryGetValue(incc, out var listeners)) { + var l = listeners.ToList(); + if (Dispatcher.UIThread.CheckAccess()) { - Notify(incc, e, listeners); + Notify(incc, e, l); } else { var inccCapture = incc; var eCapture = e; - var listenersCapture = listeners; - Dispatcher.UIThread.Post(() => Notify(inccCapture, eCapture, listenersCapture)); + Dispatcher.UIThread.Post(() => Notify(inccCapture, eCapture, l)); } } }