When using an INotifyCollectionChanged (like ObservableCollection<T>), RadDocking subscribes to its CollectionChanged event. This happens on PropertyChanged of the PaneSource property.
It seems that WPF re-intializes the application when you connect to a running remote desktop session or switch the user to a session where the corresponding WPF app is already opened. This triggers the PropertyChanged event again, which subscribes to the PaneSource collection again.
If you connect to the session multiple times, the CollectionChanged handler will be attached multiple times leading to a memory leak.
To work this around, you can manually unsubscribe from the CollectionChanged event when multiple handlers are added.
public class CustomDocking : RadDocking
{
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property.Name == nameof(PanesSource) && PanesSource is INotifyCollectionChanged observableCollection)
{
UnsubscribeCollectionChanged(observableCollection, this);
}
}
public static void UnsubscribeCollectionChanged(INotifyCollectionChanged collection, object dockingInstance)
{
var handlerMethod = typeof(RadDocking).GetMethod("OnPanesSourceCollectionChanged", BindingFlags.NonPublic | BindingFlags.Instance);
var handlerDelegate = Delegate.CreateDelegate(typeof(NotifyCollectionChangedEventHandler), dockingInstance, handlerMethod);
var field = collection.GetType().GetField("CollectionChanged", BindingFlags.Instance | BindingFlags.NonPublic);
var eventDelegate = (MulticastDelegate)field.GetValue(collection);
var handlers = eventDelegate?.GetInvocationList().Where(d => d.Method == handlerMethod);
bool shouldRemoveHandler = handlers.Count() > 1;
if (shouldRemoveHandler)
{
typeof(INotifyCollectionChanged)
.GetEvent("CollectionChanged")
?.RemoveMethod
?.Invoke(collection, new object[] { handlerDelegate });
}
}
}