Skip to content

Commit

Permalink
Fixed a bug that caused an event leak in GorgonText.
Browse files Browse the repository at this point in the history
See issue #3
  • Loading branch information
Tape-Worm committed Mar 8, 2018
1 parent d010af6 commit 3b8f506
Show file tree
Hide file tree
Showing 14 changed files with 623 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Examples/Tests/Font_EventLeak_Test/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
113 changes: 113 additions & 0 deletions Examples/Tests/Font_EventLeak_Test/Font_EventLeak_Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3F4FDE55-6FA7-435A-8AF0-620C5DB13E84}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Font_EventLeak_Test</RootNamespace>
<AssemblyName>Font_EventLeak_Test</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>5</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="SlimMath, Version=1.0.0.0, Culture=neutral, PublicKeyToken=eb5b190254be1642, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Dependencies\SlimMath\SlimMath\bin\Release\SlimMath.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="GorgonTextDebug.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Gorgon\Gorgon.Animation\Gorgon.Animation.csproj">
<Project>{7ee86743-18e9-4667-9bcb-989d5c926922}</Project>
<Name>Gorgon.Animation</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Gorgon\Gorgon.Common\Gorgon.Common.csproj">
<Project>{933569ed-52a9-4232-a929-1d4c4489b5a1}</Project>
<Name>Gorgon.Common</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Gorgon\Gorgon.Graphics\Gorgon.Graphics.csproj">
<Project>{ea4b0a1a-586a-47a4-89c1-3e6a4e821c31}</Project>
<Name>Gorgon.Graphics</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Gorgon\Gorgon.Renderers\2D\Gorgon.Renderers.2D.csproj">
<Project>{ce225f21-fb4d-4cb1-8b5b-48a61b5d0e54}</Project>
<Name>Gorgon.Renderers.2D</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
39 changes: 39 additions & 0 deletions Examples/Tests/Font_EventLeak_Test/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions Examples/Tests/Font_EventLeak_Test/Form1.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A test to ensure that the event leak for GorgonFont is fixed.
///
/// From issue https://github.com/Tape-Worm/Gorgon/issues/3
/// </summary>
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<GorgonText>(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();
}
}
}
47 changes: 47 additions & 0 deletions Examples/Tests/Font_EventLeak_Test/GorgonTextDebug.cs
Original file line number Diff line number Diff line change
@@ -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<WeakReference<GorgonText>> _objects = new List<WeakReference<GorgonText>>();
#endregion

#region Properties.
public IList<WeakReference<GorgonText>> 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
}
}
24 changes: 24 additions & 0 deletions Examples/Tests/Font_EventLeak_Test/Program.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Gorgon.Run(new Form1());
}
}
}
36 changes: 36 additions & 0 deletions Examples/Tests/Font_EventLeak_Test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
Loading

0 comments on commit 3b8f506

Please sign in to comment.