diff --git a/Examples/Tests/Font_EventLeak_Test/App.config b/Examples/Tests/Font_EventLeak_Test/App.config new file mode 100644 index 000000000..d1428ad71 --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/Examples/Tests/Font_EventLeak_Test/Font_EventLeak_Test.csproj b/Examples/Tests/Font_EventLeak_Test/Font_EventLeak_Test.csproj new file mode 100644 index 000000000..f9a91de88 --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Font_EventLeak_Test.csproj @@ -0,0 +1,113 @@ + + + + + Debug + AnyCPU + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84} + WinExe + Properties + Font_EventLeak_Test + Font_EventLeak_Test + v4.5 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + 5 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\..\..\Dependencies\SlimMath\SlimMath\bin\Release\SlimMath.dll + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + {7ee86743-18e9-4667-9bcb-989d5c926922} + Gorgon.Animation + + + {933569ed-52a9-4232-a929-1d4c4489b5a1} + Gorgon.Common + + + {ea4b0a1a-586a-47a4-89c1-3e6a4e821c31} + Gorgon.Graphics + + + {ce225f21-fb4d-4cb1-8b5b-48a61b5d0e54} + Gorgon.Renderers.2D + + + + + \ No newline at end of file diff --git a/Examples/Tests/Font_EventLeak_Test/Form1.Designer.cs b/Examples/Tests/Font_EventLeak_Test/Form1.Designer.cs new file mode 100644 index 000000000..74e2f9427 --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Form1.Designer.cs @@ -0,0 +1,39 @@ +namespace Font_EventLeak_Test +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + 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 + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Text = "Form1"; + } + + #endregion + } +} + diff --git a/Examples/Tests/Font_EventLeak_Test/Form1.cs b/Examples/Tests/Font_EventLeak_Test/Form1.cs new file mode 100644 index 000000000..7b354feaa --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Form1.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using GorgonLibrary; +using GorgonLibrary.UI; +using GorgonLibrary.Diagnostics; +using GorgonLibrary.Graphics; +using GorgonLibrary.Renderers; + +namespace Font_EventLeak_Test +{ + /// + /// A test to ensure that the event leak for GorgonFont is fixed. + /// + /// From issue https://github.com/Tape-Worm/Gorgon/issues/3 + /// + public partial class Form1 : Form + { + private GorgonGraphics _graphics; + private GorgonSwapChain _swap; + private Gorgon2D _2d; + private GorgonFont _font; + private GorgonTextDebug _debug = new GorgonTextDebug(); + private int _textCount = 0; + + protected override void OnFormClosing(FormClosingEventArgs e) + { + base.OnFormClosing(e); + + if (_font != null) + { + _font.Dispose(); + } + + if (_2d != null) + { + _2d.Dispose(); + } + + if (_swap != null) + { + _swap.Dispose(); + } + + if (_graphics != null) + { + _graphics.Dispose(); + } + } + + private bool Idle() + { + if (GorgonTiming.SecondsSinceStart > 30) + { + GorgonDialogs.InfoBox(this, string.Format("{0} text objects created. None survived.", _textCount)); + return false; + } + + ++_textCount; + // This is for testing, for the love of all that is good and holy, do NOT do this in your code. + GorgonText text = _2d.Renderables.CreateText("Test", _font, string.Format("This is a test. When object #{0} goes out of scope, the event should unbind because of WeakEventManager.", _textCount)); + + + _swap.Clear(Color.CornflowerBlue); + + text.Position = new SlimMath.Vector2(50, 50); + text.Color = Color.Yellow; + text.Draw(); + + if (_debug.Objects.Count > 512) + { + _debug.GcTest(); + } + + + _debug.Objects.Add(new WeakReference(text)); + + _2d.Render(); + _swap.Flip(1); + + return true; + } + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + Visible = true; + ClientSize = new Size(1280, 720); + + _graphics = new GorgonGraphics(); + _swap = _graphics.Output.CreateSwapChain("Swap", new GorgonSwapChainSettings + { + BufferCount = 2, + IsWindowed = true, + Format = BufferFormat.R8G8B8A8_UIntNormal, + Width = 1280, + Height = 720, + Window = this + }); + + _font = _graphics.Fonts.CreateFont("FontTest", new GorgonFontSettings + { + FontFamilyName = "Segoe UI", + FontHeightMode = FontHeightMode.Pixels, + Size = 12.0f + }); + + _2d = _graphics.Output.Create2DRenderer(_swap); + _2d.Begin2D(); + + + Gorgon.ApplicationIdleLoopMethod = Idle; + } + + public Form1() + { + InitializeComponent(); + } + } +} diff --git a/Examples/Tests/Font_EventLeak_Test/GorgonTextDebug.cs b/Examples/Tests/Font_EventLeak_Test/GorgonTextDebug.cs new file mode 100644 index 000000000..51d4af3b4 --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/GorgonTextDebug.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using GorgonLibrary.Graphics; +using GorgonLibrary.Renderers; + +namespace Font_EventLeak_Test +{ + class GorgonTextDebug + { + #region Variables. + private List> _objects = new List>(); + #endregion + + #region Properties. + public IList> Objects + { + get + { + return _objects; + } + } + #endregion + + #region Methods. + public void GcTest() + { + GC.Collect(2, GCCollectionMode.Forced, true); + GC.WaitForFullGCComplete(); + + for (int i = 0; i < _objects.Count; ++i) + { + GorgonText text; + + if (_objects[i].TryGetTarget(out text)) + { + throw new Exception(string.Format("Text object #{0} still alive. Probably still held by font.", i)); + } + } + + _objects.Clear(); + } + #endregion + } +} diff --git a/Examples/Tests/Font_EventLeak_Test/Program.cs b/Examples/Tests/Font_EventLeak_Test/Program.cs new file mode 100644 index 000000000..97992ae83 --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; +using GorgonLibrary; + +namespace Font_EventLeak_Test +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + + Gorgon.Run(new Form1()); + } + } +} diff --git a/Examples/Tests/Font_EventLeak_Test/Properties/AssemblyInfo.cs b/Examples/Tests/Font_EventLeak_Test/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..b41c21561 --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Font_EventLeak_Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Font_EventLeak_Test")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("3f4fde55-6fa7-435a-8af0-620c5db13e84")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Examples/Tests/Font_EventLeak_Test/Properties/Resources.Designer.cs b/Examples/Tests/Font_EventLeak_Test/Properties/Resources.Designer.cs new file mode 100644 index 000000000..b9c5ee452 --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Font_EventLeak_Test.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Font_EventLeak_Test.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/Examples/Tests/Font_EventLeak_Test/Properties/Resources.resx b/Examples/Tests/Font_EventLeak_Test/Properties/Resources.resx new file mode 100644 index 000000000..af7dbebba --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Examples/Tests/Font_EventLeak_Test/Properties/Settings.Designer.cs b/Examples/Tests/Font_EventLeak_Test/Properties/Settings.Designer.cs new file mode 100644 index 000000000..9c704d67c --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Font_EventLeak_Test.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/Examples/Tests/Font_EventLeak_Test/Properties/Settings.settings b/Examples/Tests/Font_EventLeak_Test/Properties/Settings.settings new file mode 100644 index 000000000..39645652a --- /dev/null +++ b/Examples/Tests/Font_EventLeak_Test/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Gorgon/Gorgon.Renderers/2D/Gorgon.Renderers.2D.csproj b/Gorgon/Gorgon.Renderers/2D/Gorgon.Renderers.2D.csproj index 866934ea6..708af7d67 100644 --- a/Gorgon/Gorgon.Renderers/2D/Gorgon.Renderers.2D.csproj +++ b/Gorgon/Gorgon.Renderers/2D/Gorgon.Renderers.2D.csproj @@ -77,6 +77,7 @@ + diff --git a/Gorgon/Gorgon.Renderers/2D/Renderables/GorgonText.cs b/Gorgon/Gorgon.Renderers/2D/Renderables/GorgonText.cs index 996dcb5e8..411d145d0 100644 --- a/Gorgon/Gorgon.Renderers/2D/Renderables/GorgonText.cs +++ b/Gorgon/Gorgon.Renderers/2D/Renderables/GorgonText.cs @@ -29,6 +29,7 @@ using System.Drawing; using System.Globalization; using System.Text; +using System.Windows; using GorgonLibrary.Animation; using GorgonLibrary.Graphics; using GorgonLibrary.Math; @@ -482,15 +483,15 @@ public GorgonFont Font if (_font != null) { - _font.FontChanged -= FontChanged; + WeakEventManager.RemoveHandler(_font, "FontChanged", FontChanged); } _font = value; if (_font != null) { - _font.FontChanged += FontChanged; - } + WeakEventManager.AddHandler(_font, "FontChanged", FontChanged); + } UpdateText(); } @@ -1524,7 +1525,7 @@ internal GorgonText(Gorgon2D gorgon2D, Gorgon2DVertexCache cache, string name, G if (font != null) { _font = font; - _font.FontChanged += FontChanged; + WeakEventManager.AddHandler(_font, "FontChanged", FontChanged); } _shadowAlpha[3] = _shadowAlpha[2] = _shadowAlpha[1] = _shadowAlpha[0] = 0.25f; diff --git a/Gorgon/Gorgon.sln b/Gorgon/Gorgon.sln index 94003dfb7..f87043a75 100644 --- a/Gorgon/Gorgon.sln +++ b/Gorgon/Gorgon.sln @@ -128,6 +128,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TvImageCodec", "..\Examples EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gorgon.SpriteEditor", "..\Tools\Gorgon.Editor.PlugIns\Gorgon.SpriteEditor\Gorgon.SpriteEditor.csproj", "{CDE22A30-64FA-44B6-AB07-CE1E8859FDAA}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F6B910DF-267F-4BF8-9714-F199086009C7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Font_EventLeak_Test", "..\Examples\Tests\Font_EventLeak_Test\Font_EventLeak_Test.csproj", "{3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -409,6 +413,12 @@ Global {CDE22A30-64FA-44B6-AB07-CE1E8859FDAA}.Release|Any CPU.Build.0 = Release|Any CPU {CDE22A30-64FA-44B6-AB07-CE1E8859FDAA}.ReleaseAndDeploy|Any CPU.ActiveCfg = ReleaseAndDeploy|Any CPU {CDE22A30-64FA-44B6-AB07-CE1E8859FDAA}.ReleaseAndDeploy|Any CPU.Build.0 = ReleaseAndDeploy|Any CPU + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}.Release|Any CPU.Build.0 = Release|Any CPU + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}.ReleaseAndDeploy|Any CPU.ActiveCfg = Release|Any CPU + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}.ReleaseAndDeploy|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -459,5 +469,7 @@ Global {9524F1F2-8260-4203-AF0A-5457053F6BEE} = {2E124B08-EECF-44F6-9175-1580FF0BFFA8} {58398BDA-7076-410A-B73C-928E79EAE8A9} = {2E124B08-EECF-44F6-9175-1580FF0BFFA8} {CDE22A30-64FA-44B6-AB07-CE1E8859FDAA} = {94E5BF16-684A-45DA-AA0E-895972EE792B} + {F6B910DF-267F-4BF8-9714-F199086009C7} = {E656298C-C1C6-4274-87B5-A19C6BA689CB} + {3F4FDE55-6FA7-435A-8AF0-620C5DB13E84} = {F6B910DF-267F-4BF8-9714-F199086009C7} EndGlobalSection EndGlobal