Skip to content

Commit

Permalink
fix: memory leak on WPF (#2986)
Browse files Browse the repository at this point in the history
* fix: memory leak on WPF

* Update ActivationForViewFetcher.cs

* Update WpfActivationForViewFetcherTest.cs

* Update WpfActivationForViewFetcherTest.cs

* Update WpfActivationForViewFetcherTest.cs
  • Loading branch information
glennawatson authored Oct 30, 2021
1 parent aa50420 commit a43e99a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

using System;
using System.Reactive.Concurrency;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

using DynamicData;
Expand Down Expand Up @@ -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()
{
Expand Down
17 changes: 0 additions & 17 deletions src/ReactiveUI.Wpf/ActivationForViewFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ public IObservable<bool> GetActivationForView(IActivatableView view)

var windowActivation = GetActivationForWindow(view);

var dispatcherActivation = GetActivationForDispatcher(fe);

return viewLoaded
.Merge(viewUnloaded)
.Merge(hitTestVisible)
.Merge(windowActivation)
.Merge(dispatcherActivation)
.DistinctUntilChanged();
}

Expand All @@ -86,19 +83,5 @@ private static IObservable<bool> GetActivationForWindow(IActivatableView view)

return viewClosed;
}

private static IObservable<bool> GetActivationForDispatcher(DispatcherObject dispatcherObject)
{
var dispatcherShutdownStarted = Observable.FromEvent<EventHandler, bool>(
eventHandler =>
{
void Handler(object? sender, EventArgs e) => eventHandler(false);
return Handler;
},
x => dispatcherObject.Dispatcher.ShutdownStarted += x,
x => dispatcherObject.Dispatcher.ShutdownStarted -= x);

return dispatcherShutdownStarted;
}
}
}

0 comments on commit a43e99a

Please sign in to comment.