[英]WPF TextBlock memory leak when using Font
I'm using .NET 4.5 on Windows 7, and might find a memory leak. 我在Windows 7上使用.NET 4.5,可能会发现内存泄漏。
I have a TextBlock
(not TextBox
- it's not the Undo problem), which changes its value every second (CPU usage, time, etc...). 我有一个TextBlock
(不是TextBox
- 它不是撤销问题),它每秒都会改变它的值(CPU使用率,时间等等)。
Using .NET Memory Profiler
(and by simply watching task manager) I noticed the memory keeps on growing. 使用.NET Memory Profiler
(通过简单地观察任务管理器)我注意到内存不断增长。 To be more accurate, I see more and more live instances of UnmanagedMemoryStream
(I tried GC.Collect()
which obviously didn't do anything). 为了更准确,我看到越来越多的UnmanagedMemoryStream
实时实例(我尝试了GC.Collect()
显然没有做任何事情)。
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: 经过一些测试,我发现只有当我将TextBlock
字体设置为资源字体时,才会出现此问题,如下所示:
<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. 我尝试直接从代码或通过Binding更新Text
属性,它对两种方式都表现相同。
Bottom line: 底线:
When the FontFamily
is set, instances of UnmanagedMemoryStream
keep on coming (forever) each time I'm updating the text. 设置FontFamily
,每次我更新文本时, UnmanagedMemoryStream
实例都会继续(永远)。 When I don't (set the FontFamily
property), memory is stable. 当我没有(设置FontFamily
属性)时,内存是稳定的。
(BTW, it happens when I use Label
instead of TextBlock
as well) (顺便说一句,当我使用Label
而不是TextBlock
时会发生这种情况)
It looks like a memory leak but I couldn't find any reference about it. 它看起来像一个内存泄漏,但我找不到任何关于它的参考。
Any suggestions of how can it be solved? 有关如何解决的任何建议?
A FontFamily
leaks UnmanagedMemoryStreams
when it is used if it was sourced from an embedded resource or a relative path. 如果FontFamily
是从嵌入式资源或相对路径获取的,则在使用时会泄漏UnmanagedMemoryStreams
。 When the FontFamily
is sourced from a system font or absolute path, it does not leak. 当FontFamily
源自系统字体或绝对路径时,它不会泄漏。
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. 对于相对路径字体:改为解析并使用绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.