Reputation: 39
I'm writing a library in C# that allows me to convert HTML to PDF. Obviously the idea is that it is cross-platform and why I'm doing with mono. To do this I have to load Seller fonts with System.Drawing.Text.PrivateFontCollection
class.
When the application finishes executing all the code, the application unexpectedly quits. After many tests, I realized that the problem is when it is called the Dispose method System.Drawing.Text.PrivateFontCollection
or when Dispose()
of System.Drawing.FontFamily
is called.
This problem is in Windows (I have Windows 7 32 bit), in linux I have no problem.
This is the test code
using System;
using System.Drawing.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
namespace FORM
{
class MainClass
{
public static void Main (string[] args)
{
PrivateFontCollection pf = new PrivateFontCollection ();
IntPtr fontBuffer = IntPtr.Zero;
pf.AddFontFile ("C:\\Users\\James\\Downloads\\open sans\\open-sans.regular.ttf");
Font f = new Font (pf.Families[0],12,FontStyle.Regular);
try {
pf.Dispose ();
}
catch{
}
pf = null;
Console.WriteLine ("Hello World!");
Console.ReadLine ();
//pf.Dispose ();
}
}
}
Upvotes: 0
Views: 832
Reputation: 6764
Are you always calling Dispose
?
You need to always call Dispose
when using unmanaged resources.
Another way of calling Dispose is with the using
keyword...
Example (before running this, do a restart on your pc to make sure all resources have been freed):
using System;
using System.Drawing.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
namespace FORM
{
class MainClass
{
public static void Main (string[] args)
{
using (PrivateFontCollection pf = new PrivateFontCollection())
{
IntPtr fontBuffer = IntPtr.Zero;
pf.AddFontFile("C:\\Windows\\Fonts\\times.ttf");
Font f = new Font(pf.Families[0], 12, FontStyle.Regular);
}
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
}
Upvotes: 0