From f0800fe11252102b0fa16aabbd85d857d0ac81c8 Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Fri, 26 May 2017 00:49:28 -0700 Subject: [PATCH 1/2] Properly dispose of font loader --- Desktop/DirectWrite/CustomFont/CustomFont.cs | 14 ++++++++++++++ .../DirectWrite/CustomFont/ResourceFontLoader.cs | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Desktop/DirectWrite/CustomFont/CustomFont.cs b/Desktop/DirectWrite/CustomFont/CustomFont.cs index 3534ba0d..5d515be3 100644 --- a/Desktop/DirectWrite/CustomFont/CustomFont.cs +++ b/Desktop/DirectWrite/CustomFont/CustomFont.cs @@ -218,5 +218,19 @@ private void comboBoxFonts_SelectedIndexChanged(object sender, EventArgs e) } + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + + CurrentResourceFontLoader.Dispose(); + } + base.Dispose(disposing); + } } } diff --git a/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs b/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs index ac5647ec..6c90cf26 100644 --- a/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs +++ b/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs @@ -110,5 +110,17 @@ FontFileStream FontFileLoader.CreateStreamFromKey(DataPointer fontFileReferenceK var index = Utilities.Read(fontFileReferenceKey.Pointer); return _fontStreams[index]; } + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + _factory.UnregisterFontFileLoader(this); + _factory.UnregisterFontCollectionLoader(this); + + base.Dispose(disposing); + } } } \ No newline at end of file From 0df5979bc2655c3b01112b023b4a464afa0a859c Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Fri, 26 May 2017 00:52:15 -0700 Subject: [PATCH 2/2] Move dispose method into form code --- .../DirectWrite/CustomFont/CustomFont.Designer.cs | 13 ------------- Desktop/DirectWrite/CustomFont/CustomFont.cs | 7 +++++-- .../DirectWrite/CustomFont/ResourceFontLoader.cs | 7 +++++-- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/Desktop/DirectWrite/CustomFont/CustomFont.Designer.cs b/Desktop/DirectWrite/CustomFont/CustomFont.Designer.cs index ef62ff4f..3a89d666 100644 --- a/Desktop/DirectWrite/CustomFont/CustomFont.Designer.cs +++ b/Desktop/DirectWrite/CustomFont/CustomFont.Designer.cs @@ -7,19 +7,6 @@ partial class CustomFont /// private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - #region Windows Form Designer generated code /// diff --git a/Desktop/DirectWrite/CustomFont/CustomFont.cs b/Desktop/DirectWrite/CustomFont/CustomFont.cs index 5d515be3..68aa3496 100644 --- a/Desktop/DirectWrite/CustomFont/CustomFont.cs +++ b/Desktop/DirectWrite/CustomFont/CustomFont.cs @@ -224,9 +224,12 @@ private void comboBoxFonts_SelectedIndexChanged(object sender, EventArgs e) /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { - if (disposing && (components != null)) + if (disposing) { - components.Dispose(); + if (components != null) + { + components.Dispose(); + } CurrentResourceFontLoader.Dispose(); } diff --git a/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs b/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs index 6c90cf26..27d1e4a6 100644 --- a/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs +++ b/Desktop/DirectWrite/CustomFont/ResourceFontLoader.cs @@ -117,8 +117,11 @@ FontFileStream FontFileLoader.CreateStreamFromKey(DataPointer fontFileReferenceK /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { - _factory.UnregisterFontFileLoader(this); - _factory.UnregisterFontCollectionLoader(this); + if (disposing) + { + _factory.UnregisterFontFileLoader(this); + _factory.UnregisterFontCollectionLoader(this); + } base.Dispose(disposing); }