question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using embedded font resources leaks memory

See original GitHub issue
  • .NET Core Version: 7.0
  • Windows version: Windows 10 version 21H2 (OS Build 19044.2251)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc…)? No

Problem description: When a custom font is loaded as a Resource and used in the application, the application slowly leaks memory via an UnmanagedMemoryStream object that grows apparently without bounds, regardless of GC collection.

The issue does not appear to affect fonts with Build Action set to Content (although publishing fonts this way may be against their respective EULAs).

Actual behavior: When a text block is rapidly changed between a series of embedded fonts, Visual Studio’s Diagnostic Tools shows a UnmanagedMemoryStream object growing by approximately 35–40 KB per second.

Expected behavior: Once all the custom fonts are loaded, changing the FontFamily property of a TextBlock shouldn’t increase memory usage at all.

Minimal repro:

  1. Create a new WPF project (the issue can be reproduced in .NET 7.0 and .NET Framework v4.8 at least, but I can find references to it online from earlier referencing now-unavailable MS Connect posts).
  2. Add some typefaces to the project (I’ve got a combination of OTF and TTF fonts in my test app), and mark their Build Action as Resource.
  3. Add those fonts to your application resources in App.xaml (exact code will differ based on the fonts you choose):
    <Application.Resources>
      <FontFamily x:Key="Flama">./Fonts/#Flama</FontFamily>
      <FontFamily x:Key="FlamaMedium">./Fonts/#Flama Medium</FontFamily>
      <FontFamily x:Key="IntroCaps">./Fonts/#Intro Caps</FontFamily>
      <FontFamily x:Key="IntroBlackInline">./Fonts/#Intro Inline Caps</FontFamily>
      <FontFamily x:Key="Pacifico">./Fonts/#Pacifico</FontFamily>
    </Application.Resources>
    
  4. In MainWindow.xaml, create a TextBlock:
    <TextBlock x:Name="TestBlock" FontSize="48">Test block</TextBlock>
    
  5. Add a new Loaded event handler for MainWindow that cycles through your embedded fonts to use as the text block’s font family as fast as possible:
    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var resourceEnumerator = Application.Current.Resources.GetEnumerator();
        var fontChanger = new DispatcherTimer();
        fontChanger.Tick += (s, e) =>
        {
            if (!resourceEnumerator.MoveNext())
            {
                resourceEnumerator.Reset();
                resourceEnumerator.MoveNext();
            }
    
            if (resourceEnumerator.Value is FontFamily fontFamily)
            {
                TestBlock.FontFamily = fontFamily;
            }
        };
        fontChanger.Start();
    }
    
  6. Run the app and observe the memory usage steadily increasing over time. Diagnostic tools will show the largest culprit for the growth being the UnmanagedMemoryStream: wpf-font-memory-leak
  7. Change the font resources to have a Build Action of Content, and set the Copy to Output Directory property to Copy if newer, and re-run the test.
  8. Observe that now the memory usage does not appear to grow over time.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kitgrosecommented, Feb 20, 2023

@anjali-wpf I’ve produced a demonstration project using all open-source fonts.

Please see the README on that project for the specific instructions for observing the issue in the debugger too.

0reactions
Symbaicommented, Aug 18, 2023

When the workaround is to set build action to content, how can I use the fonts in xaml then?

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF TextBlock memory leak when using Font
A FontFamily leaks UnmanagedMemoryStreams when it is used if it was sourced from an embedded resource or a relative path.
Read more >
Issue with removing fonts from fallbackFontAssetTable ...
I've addressed my issue here because it looks like the fallbackFontAssetTable is not cleared correctly — at least memory leak is not present ......
Read more >
Understanding memory leaks
A memory leak refers to a situation in which allocated memory is not properly deallocated or released when it is no longer needed....
Read more >
[Solved]-WPF TextBlock memory leak when using Font-wpf
A FontFamily leaks UnmanagedMemoryStreams when it is used if it was sourced from an embedded resource or a relative path. When the FontFamily...
Read more >
Memory leak caused by getFont() and ...
Basically I have a .h file called “Interface Defines” where I store all the constants used in the painting of the UI. These...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found