Browse Source

Fix font memory leak in tests (#19514) (#19534)

* Fix font memory leak in tests (#19514)

* Revert change

---------

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
release/11.3.5
Benedikt Stebner 6 months ago
committed by GitHub
parent
commit
9c6e36c193
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      src/Avalonia.Base/Media/FontManager.cs
  2. 7
      tests/Avalonia.Skia.UnitTests/Media/CustomFontManagerImpl.cs
  3. 2
      tests/Avalonia.UnitTests/UnitTestApplication.cs

11
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. /// 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. /// It is also responsible for the font fallback.
/// </summary> /// </summary>
public sealed class FontManager public sealed class FontManager : IDisposable
{ {
internal static Uri SystemFontsKey = new Uri("fonts:SystemFonts", UriKind.Absolute); internal static Uri SystemFontsKey = new Uri("fonts:SystemFonts", UriKind.Absolute);
@ -410,5 +410,14 @@ namespace Avalonia.Media
return defaultFontFamilyName; return defaultFontFamilyName;
} }
void IDisposable.Dispose()
{
foreach (var pair in _fontCollections)
pair.Value.Dispose();
_fontCollections.Clear();
(PlatformImpl as IDisposable)?.Dispose();
}
} }
} }

7
tests/Avalonia.Skia.UnitTests/Media/CustomFontManagerImpl.cs

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

2
tests/Avalonia.UnitTests/UnitTestApplication.cs

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

Loading…
Cancel
Save