From ae37ccd53e6edf96de2ac63e034535914fbdf8f6 Mon Sep 17 00:00:00 2001 From: Shaojun Li Date: Tue, 3 Jun 2025 14:20:27 +0800 Subject: [PATCH] Fix infinite animation memory leak. (#18980) --- src/Avalonia.Base/Styling/StyleInstance.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Styling/StyleInstance.cs b/src/Avalonia.Base/Styling/StyleInstance.cs index bfccfa8e83..c397aef8c6 100644 --- a/src/Avalonia.Base/Styling/StyleInstance.cs +++ b/src/Avalonia.Base/Styling/StyleInstance.cs @@ -25,6 +25,7 @@ namespace Avalonia.Styling private bool _isActive; private List? _setters; private List? _animations; + private List? _animationApplyDisposables; private LightweightSubject? _animationTrigger; public StyleInstance( @@ -69,8 +70,9 @@ namespace Avalonia.Styling if (_animations is not null && control is Animatable animatable) { _animationTrigger ??= new LightweightSubject(); + _animationApplyDisposables ??= new List(); foreach (var animation in _animations) - animation.Apply(animatable, null, _animationTrigger); + _animationApplyDisposables.Add(animation.Apply(animatable, null, _animationTrigger)); if (_activator is null) _animationTrigger.OnNext(true); @@ -81,6 +83,13 @@ namespace Avalonia.Styling { base.Dispose(); _activator?.Dispose(); + if (_animationApplyDisposables != null) + { + foreach (var item in _animationApplyDisposables) + { + item.Dispose(); + } + } } public new void MakeShared() => base.MakeShared();