dotnet / docs-desktop

This repository contains .NET documentation related to Windows Forms (winforms) and Windows Presentation Foundation (WPF).
Creative Commons Attribution 4.0 International
193 stars 175 forks source link

[doc bug]: The PrivateFontColleciton example should have a using statement because it is unmanaged code implementing IDisposable and not disposing causes issues #1872

Open k290 opened 1 month ago

k290 commented 1 month ago

Type of issue

Missing information

Description

We've had a memory leak in our application and strange file-locks for a long time.

We recently found the culprit to be the PrivateFontCollection

We found similar code in the example in the linked article. We have a suspicion we had used this article to figure out how to write the code.

In the example in the attached link PrivateFontCollection is instantiated as PrivateFontCollection privateFontCollection = new PrivateFontCollection();

However, since it is an IDisposable with very clear cleanup code in its' own and its' base class's Dispose method surely the example should be done correctly:

using(var privateFontCollection = new PrivateFontCollection)
{

}

Given the length of the example, the indentation won't be ideal. But better to show its usage correctly than to have countless projects with unmanaged code not disposed of correctly.

Its also worth noting that although the documentation in question is filed under Winforms docs, PrivateFontCollection is in System.Drawing and can be used in other applications. In our case we aren't using WinForms in particular, but this is the documentation that comes up when searching how to use PrivateFontCollection

The VB code example will also need to be corrected

Page URL

https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-create-a-private-font-collection?view=netframeworkdesktop-4.8#example

Content source URL

https://github.com/dotnet/docs-desktop/blob/main/dotnet-desktop-guide/framework/winforms/advanced/how-to-create-a-private-font-collection.md

Document Version Independent Id

d5c0beca-4d9c-5e2b-6869-6c50e4191402

Article author

@adegeo


Associated WorkItem - 291897

adegeo commented 1 month ago

Thank you for reporting this. I think System.Drawing was documented with WinForms because it's the primary scenario of why it was created. I think general a lot of examples don't show using and disposing, which can be confusing.