From a43e99ae2fbcd644460f245f471f573b7d3b66ec Mon Sep 17 00:00:00 2001 From: Glenn <5834289+glennawatson@users.noreply.github.com> Date: Sat, 30 Oct 2021 19:24:14 +1100 Subject: [PATCH] fix: memory leak on WPF (#2986) * fix: memory leak on WPF * Update ActivationForViewFetcher.cs * Update WpfActivationForViewFetcherTest.cs * Update WpfActivationForViewFetcherTest.cs * Update WpfActivationForViewFetcherTest.cs --- .../wpf/WpfActivationForViewFetcherTest.cs | 54 ------------------- .../ActivationForViewFetcher.cs | 17 ------ 2 files changed, 71 deletions(-) diff --git a/src/ReactiveUI.Tests/Platforms/wpf/WpfActivationForViewFetcherTest.cs b/src/ReactiveUI.Tests/Platforms/wpf/WpfActivationForViewFetcherTest.cs index fc99631cf8..6f1b30e38e 100644 --- a/src/ReactiveUI.Tests/Platforms/wpf/WpfActivationForViewFetcherTest.cs +++ b/src/ReactiveUI.Tests/Platforms/wpf/WpfActivationForViewFetcherTest.cs @@ -5,8 +5,6 @@ using System; using System.Reactive.Concurrency; -using System.Threading; -using System.Threading.Tasks; using System.Windows; using DynamicData; @@ -42,58 +40,6 @@ public void FrameworkElementIsActivatedAndDeactivated() new[] { true, false }.AssertAreEqual(activated); } - [Fact] - public void WindowIsActivatedAndDeactivated() - { - var window = new WpfTestWindow(); - var activation = new ActivationForViewFetcher(); - - var obs = activation.GetActivationForView(window); - obs.ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var activated).Subscribe(); - - var loaded = new RoutedEventArgs(); - loaded.RoutedEvent = FrameworkElement.LoadedEvent; - - window.RaiseEvent(loaded); - - new[] { true }.AssertAreEqual(activated); - - window.Close(); - - new[] { true, false }.AssertAreEqual(activated); - } - - [StaFact] - public void WindowAndFrameworkElementAreActivatedAndDeactivated() - { - var window = new WpfTestWindow(); - var uc = new WpfTestUserControl(); - - window.RootGrid.Children.Add(uc); - - var activation = new ActivationForViewFetcher(); - - var windowObs = activation.GetActivationForView(window); - windowObs.ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var windowActivated).Subscribe(); - - var ucObs = activation.GetActivationForView(uc); - ucObs.ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var controlActivated).Subscribe(); - - var loaded = new RoutedEventArgs(); - loaded.RoutedEvent = FrameworkElement.LoadedEvent; - - window.RaiseEvent(loaded); - uc.RaiseEvent(loaded); - - new[] { true }.AssertAreEqual(windowActivated); - new[] { true }.AssertAreEqual(controlActivated); - - window.Dispatcher.InvokeShutdown(); - - new[] { true, false }.AssertAreEqual(windowActivated); - new[] { true, false }.AssertAreEqual(controlActivated); - } - [Fact] public void IsHitTestVisibleActivatesFrameworkElement() { diff --git a/src/ReactiveUI.Wpf/ActivationForViewFetcher.cs b/src/ReactiveUI.Wpf/ActivationForViewFetcher.cs index b07cff5592..b77815fa9d 100644 --- a/src/ReactiveUI.Wpf/ActivationForViewFetcher.cs +++ b/src/ReactiveUI.Wpf/ActivationForViewFetcher.cs @@ -58,13 +58,10 @@ public IObservable GetActivationForView(IActivatableView view) var windowActivation = GetActivationForWindow(view); - var dispatcherActivation = GetActivationForDispatcher(fe); - return viewLoaded .Merge(viewUnloaded) .Merge(hitTestVisible) .Merge(windowActivation) - .Merge(dispatcherActivation) .DistinctUntilChanged(); } @@ -86,19 +83,5 @@ private static IObservable GetActivationForWindow(IActivatableView view) return viewClosed; } - - private static IObservable GetActivationForDispatcher(DispatcherObject dispatcherObject) - { - var dispatcherShutdownStarted = Observable.FromEvent( - eventHandler => - { - void Handler(object? sender, EventArgs e) => eventHandler(false); - return Handler; - }, - x => dispatcherObject.Dispatcher.ShutdownStarted += x, - x => dispatcherObject.Dispatcher.ShutdownStarted -= x); - - return dispatcherShutdownStarted; - } } }