Skip to content

Latest commit

 

History

History
49 lines (31 loc) · 2.13 KB

winforms-PrivateFontCollection-Font-handle-leak-fix.md

File metadata and controls

49 lines (31 loc) · 2.13 KB

PrivateFontCollection.AddFontFile method releases Font resources

Scope

Edge

Version Introduced

4.7.2

Source Analyzer Status

NotPlanned

Change Description

In the .NET Framework 4.7.1 and previous versions, the xref:System.Drawing.Text.PrivateFontCollection?displayProperty=nameWithType class does not release the GDI+ font resources after the xref:System.Drawing.Text.PrivateFontCollection is disposed for xref:System.Drawing.Font objects that are added to this collection using the xref:System.Drawing.Text.PrivateFontCollection.AddFontFile(System.String) method. In the .NET Framework 4.7.2 and later PrivateFontCollection.Dispose releases the GDI+ fonts that were added to the collection as files.

  • Quirked
  • Build-time break

Recommended Action

How to opt in or out of these changes

In order for an application to benefit from these changes, it must run on the .NET Framework 4.7.2 or later. The application can benefit from these changes in either of the following ways:

  • It is recompiled to target the .NET Framework 4.7.2. This change is enabled by default on Windows Forms applications that target the .NET Framework 4.7.2 or later.
  • It targets the .NET Framework 4.7.1 or an earlier version and opts out of the legacy accessibility behaviors by adding the following AppContext Switch to the <runtime> section of the app.config file and setting it to false, as the following example shows.
<runtime>
  <AppContextSwitchOverrides value="Switch.System.Drawing.Text.DoNotRemoveGdiFontsResourcesFromFontCollection=false"/>
</runtime>

Applications that target the .NET Framework 4.7.2 or later, and want to preserve the legacy behavior can opt in to not release font resources by explicitly setting this AppContext switch to true.

Affected APIs

  • M:System.Drawing.Text.PrivateFontCollection.AddFontFile(System.String)
  • M:System.Drawing.Text.FontCollection.Dispose

Category

Windows Forms