16

I'm using .NET 4.5 on Windows 7, and might find a memory leak.
I have a TextBlock (not TextBox - it's not the Undo problem), which changes its value every second (CPU usage, time, etc...).
Using .NET Memory Profiler (and by simply watching task manager) I noticed the memory keeps on growing. To be more accurate, I see more and more live instances of UnmanagedMemoryStream (I tried GC.Collect() which obviously didn't do anything).

After some tests, I've found out that this problem appears only when I'm setting the TextBlock font to a resource font as follows:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Control.Foreground" Value="#CCCCCC"/>
    <Setter Property="FontFamily" Value="pack://application:,,,/MyUIControls;component/./Fonts/#Noto Sans"/>
</Style>

I tried updating the Text property directly from code or via Binding, it behaves the same for both ways.

Bottom line:
When the FontFamily is set, instances of UnmanagedMemoryStream keep on coming (forever) each time I'm updating the text. When I don't (set the FontFamily property), memory is stable.
(BTW, it happens when I use Label instead of TextBlock as well)

It looks like a memory leak but I couldn't find any reference about it.
Any suggestions of how can it be solved?

RoeeK
  • 1,112
  • 12
  • 23
  • I know that it's an old question, but out of curiosity - have you tried to put the font resource into the app resource dictionary? Does it leak as well? `pack://application:,,,/MyUIControls;component/./Fonts/#Noto Sans` and then use it `` – Endrju Dec 23 '17 at 21:35
  • As @Endrju 's suggestion essentially creates a singleton of that font family object hold in a resource dict, it sould solve the memory issue. – Robetto Jan 15 '19 at 10:59

1 Answers1

19

A FontFamily leaks UnmanagedMemoryStreams when it is used if it was sourced from an embedded resource or a relative path. When the FontFamily is sourced from a system font or absolute path, it does not leak.

You can look here and download the project that reproduces the problem.

Workaround: For Resource fonts: save fonts into a temporary folder and use the absolute path to the stored font. For relative path fonts: resolve and use the absolute path instead.

RoeeK
  • 1,112
  • 12
  • 23
israel altar
  • 1,768
  • 1
  • 16
  • 24
  • 1
    Weird. Is that a bug or can you dispose the font? – Aron Jul 16 '15 at 11:37
  • @Aron : Look in the comments of the wpf team they say it is – israel altar Jul 16 '15 at 11:39
  • 3
    Holy Moly, and they aren't gonna fix it (focus bugs with higher impact) which actually means: focus on bugs developers can notice more easily... – RoeeK Jul 16 '15 at 11:40
  • 4
    Is it possible to use custom fonts as Resource with absolute path? Or we should forget to use custom fonts as an embedded resource in general? – Zsolt Feb 09 '16 at 10:33
  • 1
    The link to the project that reproduces the problem is broken. I can't find it on Microsoft Connect. Anyone got a working link or can show how to reproduce? – Magnus Lindhe Jul 10 '17 at 22:32
  • 1
    I have tried to come up with an alternative solution via `AddFontMemResourceEx` as detailed in https://stackoverflow.com/questions/50964801/wpf-use-font-installed-with-addfontmemresourceex-for-process-only – nietras Jun 21 '18 at 09:34