Index: Core/Common/src/Core.Common.Util/Drawing/FontHelper.cs =================================================================== diff -u -rbc686806c1d1faa65d427d1a3455610d66164dfd -r8a2cd31955ec28ee203d506300caa6a89cddb897 --- Core/Common/src/Core.Common.Util/Drawing/FontHelper.cs (.../FontHelper.cs) (revision bc686806c1d1faa65d427d1a3455610d66164dfd) +++ Core/Common/src/Core.Common.Util/Drawing/FontHelper.cs (.../FontHelper.cs) (revision 8a2cd31955ec28ee203d506300caa6a89cddb897) @@ -22,6 +22,7 @@ using System; using System.Drawing; using System.Drawing.Text; +using System.Linq; using System.Runtime.InteropServices; namespace Core.Common.Util.Drawing @@ -35,27 +36,31 @@ /// Creates a based on the provided byte array. /// /// The data to create the from. + /// The container that will become the owner of the created . /// The created . - /// Thrown when is null. - public static Font CreateFont(byte[] fontData) + /// Thrown when any input parameter is null. + /// The should be in scope (alive) at the calling side in order to prevent unintended garbage collection. + public static Font CreateFont(byte[] fontData, PrivateFontCollection privateFontCollection) { if (fontData == null) { throw new ArgumentNullException(nameof(fontData)); } + if (privateFontCollection == null) + { + throw new ArgumentNullException(nameof(privateFontCollection)); + } + uint dummy = 0; - using (var fonts = new PrivateFontCollection()) - { - IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length); - Marshal.Copy(fontData, 0, fontPtr, fontData.Length); - fonts.AddMemoryFont(fontPtr, fontData.Length); - AddFontMemResourceEx(fontPtr, (uint) fontData.Length, IntPtr.Zero, ref dummy); - Marshal.FreeCoTaskMem(fontPtr); + IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length); + Marshal.Copy(fontData, 0, fontPtr, fontData.Length); + privateFontCollection.AddMemoryFont(fontPtr, fontData.Length); + AddFontMemResourceEx(fontPtr, (uint) fontData.Length, IntPtr.Zero, ref dummy); + Marshal.FreeCoTaskMem(fontPtr); - return new Font(fonts.Families[0], 14.0F); - } + return new Font(privateFontCollection.Families.Last(), 14.0F); } [DllImport("gdi32.dll")] Index: Core/Common/test/Core.Common.Util.Test/Drawing/FontHelperTest.cs =================================================================== diff -u -rbc686806c1d1faa65d427d1a3455610d66164dfd -r8a2cd31955ec28ee203d506300caa6a89cddb897 --- Core/Common/test/Core.Common.Util.Test/Drawing/FontHelperTest.cs (.../FontHelperTest.cs) (revision bc686806c1d1faa65d427d1a3455610d66164dfd) +++ Core/Common/test/Core.Common.Util.Test/Drawing/FontHelperTest.cs (.../FontHelperTest.cs) (revision 8a2cd31955ec28ee203d506300caa6a89cddb897) @@ -21,6 +21,7 @@ using System; using System.Drawing; +using System.Drawing.Text; using Core.Common.Util.Drawing; using Core.Common.Util.Test.Properties; using NUnit.Framework; @@ -33,23 +34,38 @@ [Test] public void CreateFont_ValidFontData_CreatesExpectedFont() { + // Setup + var privateFontCollection = new PrivateFontCollection(); + // Call - Font font = FontHelper.CreateFont(Resources.ValidFont); + Font font = FontHelper.CreateFont(Resources.ValidFont, privateFontCollection); // Assert Assert.IsNotNull(font); Assert.AreEqual(14, font.Size); + Assert.AreEqual(1, privateFontCollection.Families.Length); } [Test] public void CreateFont_FontDataNull_ThrowsArgumentNullException() { // Call - void Call() => FontHelper.CreateFont(null); + void Call() => FontHelper.CreateFont(null, new PrivateFontCollection()); // Assert var exception = Assert.Throws(Call); Assert.AreEqual("fontData", exception.ParamName); } + + [Test] + public void CreateFont_PrivateFontCollectionNull_ThrowsArgumentNullException() + { + // Call + void Call() => FontHelper.CreateFont(Resources.ValidFont, null); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("privateFontCollection", exception.ParamName); + } } } \ No newline at end of file Index: Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs =================================================================== diff -u -rd9173ced08a2dabf568f4ae70e11172c16340112 -r8a2cd31955ec28ee203d506300caa6a89cddb897 --- Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision d9173ced08a2dabf568f4ae70e11172c16340112) +++ Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision 8a2cd31955ec28ee203d506300caa6a89cddb897) @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Drawing.Text; using System.Linq; using System.Windows.Forms; using Core.Common.Base; @@ -48,7 +49,10 @@ public partial class MapControl : UserControl, IMapControl { private const int updateTimerInterval = 10; - private static readonly Font font = FontHelper.CreateFont(Resources.Symbols); + + private static readonly PrivateFontCollection privateFontCollection = new PrivateFontCollection(); + private static readonly Font font = FontHelper.CreateFont(Resources.Symbols, privateFontCollection); + private readonly ILog log = LogManager.GetLogger(typeof(MapControl)); private readonly Cursor defaultCursor = Cursors.Default; private readonly RecursiveObserver mapDataCollectionObserver; Index: Core/Components/src/Core.Components.OxyPlot.Forms/ChartControl.cs =================================================================== diff -u -rd9173ced08a2dabf568f4ae70e11172c16340112 -r8a2cd31955ec28ee203d506300caa6a89cddb897 --- Core/Components/src/Core.Components.OxyPlot.Forms/ChartControl.cs (.../ChartControl.cs) (revision d9173ced08a2dabf568f4ae70e11172c16340112) +++ Core/Components/src/Core.Components.OxyPlot.Forms/ChartControl.cs (.../ChartControl.cs) (revision 8a2cd31955ec28ee203d506300caa6a89cddb897) @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Drawing.Text; using System.Linq; using System.Windows.Forms; using Core.Common.Base; @@ -39,7 +40,9 @@ /// public partial class ChartControl : UserControl, IChartControl { - private static readonly Font font = FontHelper.CreateFont(Resources.Symbols); + private static readonly PrivateFontCollection privateFontCollection = new PrivateFontCollection(); + private static readonly Font font = FontHelper.CreateFont(Resources.Symbols, privateFontCollection); + private readonly RecursiveObserver chartDataCollectionObserver; private readonly List drawnChartDataList = new List();