Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FontSource/FontFileStream embedded resource memory leak #9948

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

h3xds1nz
Copy link
Member

@h3xds1nz h3xds1nz commented Oct 12, 2024

Fixes #7289

Description

Fixes memory leak when using fonts from embedded resources that are materialized onto FontSource -> FontFamily, etc.

The issue is basically in FontSource.GetUnmanagedStream():

  • We're passing around pure UnmanagedMemoryStream instance, but life without destructor is kind of hard.
    • This can be removed once FontFileStream from DirectWriteForwarder would be migrated to C#.

More context: The fix could be done by disposing the UnmanagedMemoryStream instantly and wrapping it in a new instance so it's cleared from the List<Stream> of PackagePart but I think it's nice to tie the lifetime together.

Customer Impact

Customers will finally get their fix after 10 years.

https://stackoverflow.com/questions/31452443/wpf-textblock-memory-leak-when-using-font
https://stackoverflow.com/questions/49550759/wpf-load-font-from-resources
https://stackoverflow.com/questions/45297837/what-causes-unmanagedmemorystream-in-wpf-memory-leak
https://stackoverflow.com/questions/39157846/wpf-glyphs-control-font-uriits-slow-and-remains-in-memory

Regression

Has been around since at least .NET 4.5.

Testing

Using the repro in #7289; sample apps use.

Risk

Low though we're passing around stuff onto C++/CLI, that's always awful.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested review from a team as code owners October 12, 2024 22:52
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Oct 12, 2024
@h3xds1nz h3xds1nz changed the title Fix FontSource/FontResourceStream embedded resource memory leak Fix FontSource/FontFileStream embedded resource memory leak Oct 13, 2024
@Symbai
Copy link
Contributor

Symbai commented Oct 13, 2024

Customers will finally get their fix after 10 years.

❤️❤️❤️

@miloush
Copy link
Contributor

miloush commented Oct 14, 2024

LGTM

@poutine70
Copy link

What is the status of this issue? Anyfix soon?

himgoyalmicro
himgoyalmicro previously approved these changes Mar 11, 2025
Copy link
Contributor

@himgoyalmicro himgoyalmicro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@himgoyalmicro
Copy link
Contributor

@h3xds1nz this PR is good to be merged. Can you please resolve the merge conflicts and the conversations.

@h3xds1nz
Copy link
Member Author

@himgoyalmicro Resolved :)

P.S. I'd consider backporting this to NetFX given that you've backported the XPS one as well. For some apps this is rather a big issue.

Copy link

codecov bot commented Mar 11, 2025

Codecov Report

Attention: Patch coverage is 0% with 17 lines in your changes missing coverage. Please review.

Project coverage is 11.41724%. Comparing base (73c7e63) to head (fbaf8b5).

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main       #9948         +/-   ##
===================================================
- Coverage   11.55182%   11.41724%   -0.13458%     
===================================================
  Files           3214        3214                 
  Lines         648495      648502          +7     
  Branches       71512       71510          -2     
===================================================
- Hits           74913       74041        -872     
- Misses        572419      573303        +884     
+ Partials        1163        1158          -5     
Flag Coverage Δ
Debug 11.41724% <0.00000%> (-0.13458%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions Included in test pass PR metadata: Label to tag PRs, to facilitate with triage Status:Completed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using embedded font resources leaks memory
6 participants