浏览代码

Fix font memory leak in tests (#19514)

Julien Lebosquain 1 月之前
父节点
当前提交
a8fed229f6

+ 10 - 1
src/Avalonia.Base/Media/FontManager.cs

@@ -15,7 +15,7 @@ namespace Avalonia.Media
     ///     The font manager is used to query the system's installed fonts and is responsible for caching loaded fonts.
     ///     It is also responsible for the font fallback.
     /// </summary>
-    public sealed class FontManager
+    public sealed class FontManager : IDisposable
     {
         internal static Uri SystemFontsKey = new Uri("fonts:SystemFonts", UriKind.Absolute);
 
@@ -368,5 +368,14 @@ namespace Avalonia.Media
 
             return defaultFontFamilyName;
         }
+
+        void IDisposable.Dispose()
+        {
+            foreach (var pair in _fontCollections)
+                pair.Value.Dispose();
+
+            _fontCollections.Clear();
+            (PlatformImpl as IDisposable)?.Dispose();
+        }
     }
 }

+ 6 - 1
tests/Avalonia.Skia.UnitTests/Media/CustomFontManagerImpl.cs

@@ -10,7 +10,7 @@ using System.IO;
 
 namespace Avalonia.Skia.UnitTests.Media
 {
-    public class CustomFontManagerImpl : IFontManagerImpl
+    public class CustomFontManagerImpl : IFontManagerImpl, IDisposable
     {
         private readonly string _defaultFamilyName;
         private readonly IFontCollection _customFonts;
@@ -94,5 +94,10 @@ namespace Avalonia.Skia.UnitTests.Media
 
             return true;
         }
+
+        public void Dispose()
+        {
+            _customFonts.Dispose();
+        }
     }
 }

+ 2 - 1
tests/Avalonia.UnitTests/UnitTestApplication.cs

@@ -51,7 +51,8 @@ namespace Avalonia.UnitTests
                 }
 
                 ((ToolTipService)AvaloniaLocator.Current.GetService<IToolTipService>())?.Dispose();
-                
+                (AvaloniaLocator.Current.GetService<FontManager>() as IDisposable)?.Dispose();
+
                 Dispatcher.ResetForUnitTests();
                 scope.Dispose();
                 Dispatcher.ResetBeforeUnitTests();