Skip to content

Commit ad134aa

Browse files
committed
properly dispose resources
1 parent d5c6120 commit ad134aa

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

Source/DXControl/D2Phap.DXControl.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2323
<PackageReleaseNotes>See release notes here: https://github.com/d2phap/DXControl/releases</PackageReleaseNotes>
2424
<Authors>Duong Dieu Phap</Authors>
25-
<VersionPrefix>3.2.0</VersionPrefix>
25+
<VersionPrefix>3.3.0</VersionPrefix>
2626
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2727
<EmbedUntrackedSources>true</EmbedUntrackedSources>
2828
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>

Source/DXControl/DXControl.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class DXControl : Control
2323
protected readonly PeriodicTimer _timer = new(TimeSpan.FromMilliseconds(10));
2424

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

5252

5353
/// <summary>
5454
/// Gets DirectWrite factory.
5555
/// </summary>
5656
[Browsable(false)]
57-
public IComObject<IDWriteFactory5> DirectWriteFactory => _dWriteFactory;
57+
public IComObject<IDWriteFactory5>? DirectWriteFactory => _dWriteFactory;
5858

5959

6060
/// <summary>
@@ -218,9 +218,6 @@ protected override void DestroyHandle()
218218
{
219219
base.DestroyHandle();
220220

221-
_d2DFactory.Dispose();
222-
_dWriteFactory.Dispose();
223-
224221
DisposeDevice();
225222
}
226223

@@ -272,8 +269,6 @@ protected override void WndProc(ref Message m)
272269
break;
273270

274271
case WM_DESTROY:
275-
_d2DFactory.Dispose();
276-
_dWriteFactory.Dispose();
277272
DisposeDevice();
278273

279274
base.WndProc(ref m);
@@ -480,6 +475,9 @@ private async Task StartFramingTimerAsync()
480475
/// </summary>
481476
public void CreateDevice()
482477
{
478+
_d2DFactory = D2D1Functions.D2D1CreateFactory<ID2D1Factory1>(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED);
479+
_dWriteFactory = DWriteFunctions.DWriteCreateFactory<IDWriteFactory5>(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED);
480+
483481
var renderTargetProps = new D2D1_RENDER_TARGET_PROPERTIES()
484482
{
485483
dpiX = _dpi,
@@ -517,12 +515,21 @@ public void CreateDevice()
517515
/// </summary>
518516
public void DisposeDevice()
519517
{
518+
_graphicsD2d?.Dispose();
519+
_graphicsD2d = null;
520+
520521
_device?.Dispose();
521522
_device = null;
522523

523524
_renderTarget?.Dispose();
524525
_renderTarget = null;
525526

527+
_d2DFactory?.Dispose();
528+
_d2DFactory = null;
529+
530+
_dWriteFactory?.Dispose();
531+
_dWriteFactory = null;
532+
526533
GC.Collect();
527534
}
528535

Source/DXControl/Graphics/D2DGraphics.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ protected virtual void Dispose(bool disposing)
2626
if (disposing)
2727
{
2828
// Free any other managed objects here.
29-
30-
DWriteFactory.Dispose();
31-
DeviceContext.Dispose();
3229
}
3330

3431
// Free any unmanaged objects here.

Source/Demo/Demo.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.7.0" />
21-
<PackageReference Include="Magick.NET.SystemWindowsMedia" Version="7.2.3" />
20+
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.8.0" />
21+
<PackageReference Include="Magick.NET.SystemWindowsMedia" Version="7.2.4" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

0 commit comments

Comments
 (0)