diff --git a/src/Core/src/Animations/AnimationManager.cs b/src/Core/src/Animations/AnimationManager.cs index 7b1320795545..58b647a7c1d3 100644 --- a/src/Core/src/Animations/AnimationManager.cs +++ b/src/Core/src/Animations/AnimationManager.cs @@ -115,12 +115,25 @@ void OnAnimationTick(Animation animation) protected virtual void Dispose(bool disposing) { - if (!_disposedValue) + if (_disposedValue) { - if (disposing && Ticker is IDisposable disposable) - disposable.Dispose(); + return; + } + + _disposedValue = true; + + if (!disposing) + { + return; + } - _disposedValue = true; + Ticker.Fire -= OnFire; + End(); + _animations.Clear(); + + if (Ticker is IDisposable disposable) + { + disposable.Dispose(); } } diff --git a/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs new file mode 100644 index 000000000000..89387bbeed29 --- /dev/null +++ b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs @@ -0,0 +1,115 @@ +#nullable enable +using System; +using System.Runtime.CompilerServices; +using Microsoft.Maui.Animations; +using Xunit; + +namespace Microsoft.Maui.UnitTests +{ + [Category(TestCategory.Animations)] + public class AnimationManagerTests + { + [Fact] + public void DisposeReleasesOwnedTickerCallbackWithoutDisposingAnimation() + { + var ticker = new TestTicker(); + var manager = new AnimationManager(ticker); + bool stepCalled = false; + bool finishedCalled = false; + var animation = new Animation(_ => stepCalled = true, duration: 10, finished: () => finishedCalled = true); + + manager.Add(animation); + manager.Dispose(); + + Assert.Null(ticker.Fire); + Assert.False(ticker.IsRunning); + Assert.Equal(1, ticker.StopCount); + Assert.Equal(1, ticker.DisposeCount); + Assert.False(animation.IsDisposed); + Assert.NotNull(animation.Step); + Assert.NotNull(animation.Finished); + Assert.False(stepCalled); + Assert.False(finishedCalled); + } + + [Fact] + public void DisposeReleasesAnimationPayloadWhenTickerOutlivesManager() + { + var ticker = new TestTicker(); + WeakReference payload = CreateDisposedManagerPayload(ticker); + + CollectGarbage(); + + Assert.False(payload.TryGetTarget(out _)); + GC.KeepAlive(ticker); + } + + [Fact] + public void DisposeRemovesOwnedCallbackFromMulticastDelegate() + { + var ticker = new TestTicker(); + var manager = new AnimationManager(ticker); + bool stepCalled = false; + int otherCallbackCount = 0; + manager.Add(new Animation(_ => stepCalled = true, duration: 10)); + ticker.Fire += () => otherCallbackCount++; + + manager.Dispose(); + ticker.Fire?.Invoke(); + + Assert.False(stepCalled); + Assert.Equal(1, otherCallbackCount); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static WeakReference CreateDisposedManagerPayload(TestTicker ticker) + { + var payload = new object(); + var manager = new AnimationManager(ticker); + manager.Add(new Animation(_ => GC.KeepAlive(payload), duration: 10)); + manager.Dispose(); + return new(payload); + } + + static void CollectGarbage() + { + for (int i = 0; i < 3; i++) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + } + } + + class TestTicker : ITicker, IDisposable + { + public bool IsRunning { get; private set; } + + public bool SystemEnabled { get; set; } = true; + + public int MaxFps { get; set; } = 60; + + public Action? Fire { get; set; } + + public int DisposeCount { get; private set; } + + public int StopCount { get; private set; } + + public void Start() + { + IsRunning = true; + } + + public void Stop() + { + StopCount++; + IsRunning = false; + } + + public void Dispose() + { + DisposeCount++; + } + } + } +}