0

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 ();
        }
    }
}
Rob
  • 26,989
  • 16
  • 82
  • 98

1 Answers1

0

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();
        }
    }
}
bastos.sergio
  • 6,684
  • 4
  • 26
  • 36
  • PrivateFontCollection and FontFamily are IDisposable, this means that although not explicitly called the method, when the application is close the Dispose methods are called. To me, this is a mono bug, but wanted to know if anyone knows any solution while it is corrected – user3446605 Mar 21 '14 at 14:14
  • You're right... The method `Dispose` is called automatically, **but the .net framework does not guarantee when it will call it**... And there is your problem... You're most likely trying to access resources that haven't yet been freed (i.e. Dispose hasn't run yet) – bastos.sergio Mar 21 '14 at 14:18
  • No my problem its not that. Consider the code without call the Dispose method, anyway the application falls. The problem is with mono not with .net, and only in Windows, not in Linux – user3446605 Mar 21 '14 at 14:25
  • Run the code in your PC with mono and the application crash, Whether you call the dispose method or not – user3446605 Mar 21 '14 at 14:28
  • Hum, I don't really have a PC with mono to try this out... The code I posted above is tested on .net framework 4.0, though, and it generated no exceptions... Perhaps someone else can help you out... Regardless of that, you should always call dispose immediatly after using unmanaged resources (whether using mono or .net)... – bastos.sergio Mar 21 '14 at 14:32
  • Yes I know but the application crash always with mono, whether you call the dispose method or not. Thank you for you collaboration, and you have reason with .NET there aren't problem, but with MONO crash – user3446605 Mar 21 '14 at 14:36