Skip to content

Commit

Permalink
properly dispose resources
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed May 18, 2024
1 parent d5c6120 commit ad134aa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Source/DXControl/D2Phap.DXControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageReleaseNotes>See release notes here: https://github.com/d2phap/DXControl/releases</PackageReleaseNotes>
<Authors>Duong Dieu Phap</Authors>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
Expand Down
25 changes: 16 additions & 9 deletions Source/DXControl/DXControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class DXControl : Control
protected readonly PeriodicTimer _timer = new(TimeSpan.FromMilliseconds(10));

// Protected properties
protected readonly IComObject<ID2D1Factory1> _d2DFactory = D2D1Functions.D2D1CreateFactory<ID2D1Factory1>(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED);
protected readonly IComObject<IDWriteFactory5> _dWriteFactory = DWriteFunctions.DWriteCreateFactory<IDWriteFactory5>(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED);
protected IComObject<ID2D1Factory1>? _d2DFactory = null;
protected IComObject<IDWriteFactory5>? _dWriteFactory = null;
protected IComObject<ID2D1HwndRenderTarget>? _renderTarget;
protected IComObject<ID2D1DeviceContext6>? _device;
protected D2DGraphics? _graphicsD2d;
Expand All @@ -47,14 +47,14 @@ public class DXControl : Control
/// Gets Direct2D factory.
/// </summary>
[Browsable(false)]
public IComObject<ID2D1Factory1> Direct2DFactory => _d2DFactory;
public IComObject<ID2D1Factory1>? Direct2DFactory => _d2DFactory;


/// <summary>
/// Gets DirectWrite factory.
/// </summary>
[Browsable(false)]
public IComObject<IDWriteFactory5> DirectWriteFactory => _dWriteFactory;
public IComObject<IDWriteFactory5>? DirectWriteFactory => _dWriteFactory;


/// <summary>
Expand Down Expand Up @@ -218,9 +218,6 @@ protected override void DestroyHandle()
{
base.DestroyHandle();

_d2DFactory.Dispose();
_dWriteFactory.Dispose();

DisposeDevice();
}

Expand Down Expand Up @@ -272,8 +269,6 @@ protected override void WndProc(ref Message m)
break;

case WM_DESTROY:
_d2DFactory.Dispose();
_dWriteFactory.Dispose();
DisposeDevice();

base.WndProc(ref m);
Expand Down Expand Up @@ -480,6 +475,9 @@ private async Task StartFramingTimerAsync()
/// </summary>
public void CreateDevice()
{
_d2DFactory = D2D1Functions.D2D1CreateFactory<ID2D1Factory1>(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED);
_dWriteFactory = DWriteFunctions.DWriteCreateFactory<IDWriteFactory5>(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED);

var renderTargetProps = new D2D1_RENDER_TARGET_PROPERTIES()
{
dpiX = _dpi,
Expand Down Expand Up @@ -517,12 +515,21 @@ public void CreateDevice()
/// </summary>
public void DisposeDevice()
{
_graphicsD2d?.Dispose();
_graphicsD2d = null;

_device?.Dispose();
_device = null;

_renderTarget?.Dispose();
_renderTarget = null;

_d2DFactory?.Dispose();
_d2DFactory = null;

_dWriteFactory?.Dispose();
_dWriteFactory = null;

GC.Collect();
}

Expand Down
3 changes: 0 additions & 3 deletions Source/DXControl/Graphics/D2DGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
// Free any other managed objects here.

DWriteFactory.Dispose();
DeviceContext.Dispose();
}

// Free any unmanaged objects here.
Expand Down
4 changes: 2 additions & 2 deletions Source/Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.7.0" />
<PackageReference Include="Magick.NET.SystemWindowsMedia" Version="7.2.3" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.8.0" />
<PackageReference Include="Magick.NET.SystemWindowsMedia" Version="7.2.4" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit ad134aa

Please sign in to comment.