From 92d0455e8e5ccd40c9ef2f744ef3ecf2e3babed8 Mon Sep 17 00:00:00 2001 From: HarishwaranVijayakumar Date: Tue, 11 Aug 2026 14:02:19 +0530 Subject: [PATCH 1/5] Add fix --- src/Core/src/Animations/AnimationManager.cs | 43 +++++++- .../Animations/AnimationManagerTests.cs | 99 +++++++++++++++++++ 2 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs diff --git a/src/Core/src/Animations/AnimationManager.cs b/src/Core/src/Animations/AnimationManager.cs index 7b1320795545..6a07ddaae146 100644 --- a/src/Core/src/Animations/AnimationManager.cs +++ b/src/Core/src/Animations/AnimationManager.cs @@ -7,6 +7,7 @@ namespace Microsoft.Maui.Animations public class AnimationManager : IAnimationManager, IDisposable { readonly List _animations = new(); + readonly Action _fire; long _lastUpdate; bool _disposedValue; @@ -17,9 +18,10 @@ public class AnimationManager : IAnimationManager, IDisposable public AnimationManager(ITicker ticker) { _lastUpdate = GetCurrentTick(); + _fire = OnFire; Ticker = ticker; - Ticker.Fire = OnFire; + Ticker.Fire = _fire; } /// @@ -34,6 +36,11 @@ public AnimationManager(ITicker ticker) /// public void Add(Animation animation) { + if (_disposedValue) + { + return; + } + // If animations are disabled, don't do anything if (!Ticker.SystemEnabled) { @@ -49,6 +56,11 @@ public void Add(Animation animation) /// public void Remove(Animation animation) { + if (_disposedValue) + { + return; + } + _animations.TryRemove(animation); if (_animations.Count == 0) @@ -69,6 +81,11 @@ static long GetCurrentTick() => void OnFire() { + if (_disposedValue) + { + return; + } + if (!Ticker.SystemEnabled) { // This is a hack - if we're here, the ticker has detected that animations are no longer enabled, @@ -115,12 +132,28 @@ 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; + _disposedValue = true; + + if (disposing) + { + End(); + + if (ReferenceEquals(Ticker.Fire, _fire)) + { + Ticker.Fire = null; + } + + _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..ce4b47a765eb --- /dev/null +++ b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs @@ -0,0 +1,99 @@ +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); + Action fire = ticker.Fire!; + + manager.Add(animation); + manager.Dispose(); + fire(); + + 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); + } + + [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++; + } + } + } +} From e4ca6a53adb9642978d43cc8243f4f097b976560 Mon Sep 17 00:00:00 2001 From: HarishwaranVijayakumar Date: Tue, 11 Aug 2026 16:33:45 +0530 Subject: [PATCH 2/5] Update fix and add testcase --- src/Core/src/Animations/AnimationManager.cs | 30 ++++++++--------- .../Animations/AnimationManagerTests.cs | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Core/src/Animations/AnimationManager.cs b/src/Core/src/Animations/AnimationManager.cs index 6a07ddaae146..ceffa2a2275a 100644 --- a/src/Core/src/Animations/AnimationManager.cs +++ b/src/Core/src/Animations/AnimationManager.cs @@ -7,7 +7,6 @@ namespace Microsoft.Maui.Animations public class AnimationManager : IAnimationManager, IDisposable { readonly List _animations = new(); - readonly Action _fire; long _lastUpdate; bool _disposedValue; @@ -18,10 +17,9 @@ public class AnimationManager : IAnimationManager, IDisposable public AnimationManager(ITicker ticker) { _lastUpdate = GetCurrentTick(); - _fire = OnFire; Ticker = ticker; - Ticker.Fire = _fire; + Ticker.Fire = OnFire; } /// @@ -105,6 +103,11 @@ void OnFire() foreach (var animation in animations) { + if (_disposedValue) + { + return; + } + OnAnimationTick(animation); } @@ -139,21 +142,18 @@ protected virtual void Dispose(bool disposing) _disposedValue = true; - if (disposing) + if (!disposing) { - End(); - - if (ReferenceEquals(Ticker.Fire, _fire)) - { - Ticker.Fire = null; - } + return; + } - _animations.Clear(); + Ticker.Fire -= OnFire; + End(); + _animations.Clear(); - if (Ticker is IDisposable disposable) - { - disposable.Dispose(); - } + 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 index ce4b47a765eb..7f6dca2fbac7 100644 --- a/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs +++ b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs @@ -45,6 +45,38 @@ public void DisposeReleasesAnimationPayloadWhenTickerOutlivesManager() 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); + } + + [Fact] + public void DisposeDuringTickStopsProcessingRemainingAnimations() + { + var ticker = new TestTicker(); + var manager = new AnimationManager(ticker); + bool secondAnimationCalled = false; + manager.Add(new Animation(_ => manager.Dispose(), duration: 10)); + manager.Add(new Animation(_ => secondAnimationCalled = true, duration: 10)); + Action fire = ticker.Fire!; + + fire(); + + Assert.False(secondAnimationCalled); + } + [MethodImpl(MethodImplOptions.NoInlining)] static WeakReference CreateDisposedManagerPayload(TestTicker ticker) { From f118e324f1f6e1ecb33da1f2d263a444d8da87ba Mon Sep 17 00:00:00 2001 From: HarishwaranVijayakumar Date: Tue, 11 Aug 2026 16:36:20 +0530 Subject: [PATCH 3/5] Modify fix to throw exception instead of return --- src/Core/src/Animations/AnimationManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/src/Animations/AnimationManager.cs b/src/Core/src/Animations/AnimationManager.cs index ceffa2a2275a..193e811090ed 100644 --- a/src/Core/src/Animations/AnimationManager.cs +++ b/src/Core/src/Animations/AnimationManager.cs @@ -36,7 +36,7 @@ public void Add(Animation animation) { if (_disposedValue) { - return; + throw new ObjectDisposedException(nameof(AnimationManager)); } // If animations are disabled, don't do anything From 44922c2445fe18d7c3269c66d5418f78125a6352 Mon Sep 17 00:00:00 2001 From: HarishwaranVijayakumar Date: Fri, 14 Aug 2026 13:33:20 +0530 Subject: [PATCH 4/5] Modify fix --- src/Core/src/Animations/AnimationManager.cs | 20 ------------------- .../Animations/AnimationManagerTests.cs | 17 ---------------- 2 files changed, 37 deletions(-) diff --git a/src/Core/src/Animations/AnimationManager.cs b/src/Core/src/Animations/AnimationManager.cs index 193e811090ed..58b647a7c1d3 100644 --- a/src/Core/src/Animations/AnimationManager.cs +++ b/src/Core/src/Animations/AnimationManager.cs @@ -34,11 +34,6 @@ public AnimationManager(ITicker ticker) /// public void Add(Animation animation) { - if (_disposedValue) - { - throw new ObjectDisposedException(nameof(AnimationManager)); - } - // If animations are disabled, don't do anything if (!Ticker.SystemEnabled) { @@ -54,11 +49,6 @@ public void Add(Animation animation) /// public void Remove(Animation animation) { - if (_disposedValue) - { - return; - } - _animations.TryRemove(animation); if (_animations.Count == 0) @@ -79,11 +69,6 @@ static long GetCurrentTick() => void OnFire() { - if (_disposedValue) - { - return; - } - if (!Ticker.SystemEnabled) { // This is a hack - if we're here, the ticker has detected that animations are no longer enabled, @@ -103,11 +88,6 @@ void OnFire() foreach (var animation in animations) { - if (_disposedValue) - { - return; - } - OnAnimationTick(animation); } diff --git a/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs index 7f6dca2fbac7..9b7ac2619d68 100644 --- a/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs +++ b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs @@ -16,11 +16,9 @@ public void DisposeReleasesOwnedTickerCallbackWithoutDisposingAnimation() bool stepCalled = false; bool finishedCalled = false; var animation = new Animation(_ => stepCalled = true, duration: 10, finished: () => finishedCalled = true); - Action fire = ticker.Fire!; manager.Add(animation); manager.Dispose(); - fire(); Assert.Null(ticker.Fire); Assert.False(ticker.IsRunning); @@ -62,21 +60,6 @@ public void DisposeRemovesOwnedCallbackFromMulticastDelegate() Assert.Equal(1, otherCallbackCount); } - [Fact] - public void DisposeDuringTickStopsProcessingRemainingAnimations() - { - var ticker = new TestTicker(); - var manager = new AnimationManager(ticker); - bool secondAnimationCalled = false; - manager.Add(new Animation(_ => manager.Dispose(), duration: 10)); - manager.Add(new Animation(_ => secondAnimationCalled = true, duration: 10)); - Action fire = ticker.Fire!; - - fire(); - - Assert.False(secondAnimationCalled); - } - [MethodImpl(MethodImplOptions.NoInlining)] static WeakReference CreateDisposedManagerPayload(TestTicker ticker) { From dcef797b5821f30aa662b4dc5b863200c248a9e0 Mon Sep 17 00:00:00 2001 From: HarishwaranVijayakumar Date: Thu, 20 Aug 2026 10:55:58 +0530 Subject: [PATCH 5/5] Address copilot concern --- src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs index 9b7ac2619d68..89387bbeed29 100644 --- a/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs +++ b/src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Runtime.CompilerServices; using Microsoft.Maui.Animations; @@ -88,7 +89,7 @@ class TestTicker : ITicker, IDisposable public int MaxFps { get; set; } = 60; - public Action Fire { get; set; } + public Action? Fire { get; set; } public int DisposeCount { get; private set; }