8

We create a large count of fonts for a short use. The fonts are embedded in documents. I want delete the font files if not use anymore. How can we do this? The follow simplified code does not work:

PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(fontFile);
FontFamily family = pfc.Families[0];
Console.WriteLine(family.GetName(0));

family.Dispose();
pfc.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(fontFile);

The delete of the file is failing because the file is locked. What can I do else to free the file lock?

PS: Before we have use AddMemoryFont. This work with Windows 7. But with Windows 8 .NET use the wrong font files after the first FontFamily was Disposed. Because every Document can contain other fonts we need a very large count of fonts and can not hold references to all.

Horcrux7
  • 23,758
  • 21
  • 98
  • 156
  • 1
    Added connect bug https://connect.microsoft.com/VisualStudio/feedback/details/1379843 – Peter May 29 '15 at 14:00

1 Answers1

14

After looking in the code of method AddFontFile:

public void AddFontFile(string filename)
{
    IntSecurity.DemandReadFileIO(filename);
    int num = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, this.nativeFontCollection), filename);
    if (num != 0)
    {
        throw SafeNativeMethods.Gdip.StatusException(num);
    }
    SafeNativeMethods.AddFontFile(filename);
}

we see that the font is registered 2 times. First in GDI+ and in the last line in GDI32. This is different to the method AddMemoryFont. In the Dispose method it is only unregistered in GDI+. This result in a leak in GDI32.

To compensate this you can call the follow:

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int RemoveFontResourceEx(string lpszFilename, int fl, IntPtr pdv);

pfc.AddFontFile(fontFile);
RemoveFontResourceEx(fontFile, 16, IntPtr.Zero);
Jason Coyne
  • 6,509
  • 8
  • 40
  • 70
Horcrux7
  • 23,758
  • 21
  • 98
  • 156
  • 2
    For those wondering about that '16': it's the FR_PRIVATE flag. – Pieter Witvoet Nov 25 '14 at 11:04
  • 1
    If we apply your fix, what would happen if MS fixes this bug? – Peter May 29 '15 at 13:52
  • 1
    @Peter Nothing would haven if you remove a font that was not added. You can test this if you call it 2 times. Or you call it without AddFontFIle. – Horcrux7 May 30 '15 at 19:53
  • `fontFile` and `tempFile` should point to the same file path, right? – Uwe Keim Jan 11 '16 at 19:05
  • Yes, this seems a copy and paste mistake. – Horcrux7 Jan 13 '16 at 07:28
  • Thanks for your effort investigating the issue - saved me some time :-) – freakinpenguin Jul 16 '19 at 07:12
  • I think this issue is fixed in the current source code https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/PrivateFontCollection.cs,87a66acd83db7bb9, https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Gdiplus.cs,c272b6eef5f7354e – live2 Dec 31 '19 at 10:11