diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs
index 4d630cb2..9c63e38f 100644
--- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs
+++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs
@@ -9,8 +9,6 @@ using TerraFX.Interop.Windows;
using Windows.Foundation;
using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator;
-#pragma warning disable CA1063
-
namespace ComputeSharp.D2D1.Uwp;
///
@@ -28,17 +26,6 @@ unsafe partial class CanvasEffect
return GetCanvasImage().GetBounds(resourceCreator, transform);
}
- ///
- public void Dispose()
- {
- lock (this.lockObject)
- {
- this.canvasImage?.Dispose();
- this.canvasImage = null;
- this.isDisposed = true;
- }
- }
-
///
unsafe int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
{
@@ -50,7 +37,13 @@ unsafe partial class CanvasEffect
}
///
- unsafe int ICanvasImageInterop.Interface.GetD2DImage(ICanvasDevice* device, ID2D1DeviceContext* deviceContext, WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, float* realizeDpi, ID2D1Image** ppImage)
+ unsafe int ICanvasImageInterop.Interface.GetD2DImage(
+ ICanvasDevice* device,
+ ID2D1DeviceContext* deviceContext,
+ WIN2D_GET_D2D_IMAGE_FLAGS flags,
+ float targetDpi,
+ float* realizeDpi,
+ ID2D1Image** ppImage)
{
using ComPtr canvasImageInterop = default;
diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs
index 68f39a7f..7439f19e 100644
--- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs
+++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs
@@ -1,10 +1,9 @@
+using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using ABI.Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas;
-#pragma warning disable CA1063
-
namespace ComputeSharp.D2D1.Uwp;
///
@@ -25,13 +24,73 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
///
/// Indicates whether the current state has been invalidated (requiring to be called).
///
- private bool isInvalidated;
+ ///
+ /// This is initially so that will always be called when
+ /// first creating the image, even if the effect has not bee invalidated. This ensures default parameters are set.
+ ///
+ private bool isInvalidated = true;
///
/// Indicates whether the effect is disposed.
///
private bool isDisposed;
+ ///
+ public void Dispose()
+ {
+ Dispose(disposing: true);
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ /// Disposes any disposable resources in the current instance.
+ ///
+ ///
+ /// Indicates whether the method was called from of from a finalizer:
+ ///
+ /// - : the method was called from .
+ /// - : the method was called from a finalizer.
+ ///
+ ///
+ ///
+ ///
+ /// If implementing a type that derives from , this method can be overridden to add logic to dispose
+ /// any additional state. Managed objects should only be disposed when is .
+ ///
+ ///
+ /// If the derived type does not need a finalizer (ie. if it doesn't wrap any native resources), this method should not be
+ /// called directly. It will be called automatically by . If the derived type does instead define a
+ /// finalizer, it should call with a value of . As mentioned above, when
+ /// implementing in a derived type, managed objects should only be disposed when
+ /// is , otherwise only unmanaged resorces should be disposed.
+ ///
+ ///
+ /// Though the pattern does support this, it is recommended not to add finalizers in types deriving from .
+ /// Rather, if an effect needs to wrap native resources, it should do so via some same managed wrapper type (such as
+ /// ), or some other custom unmanaged resource wrapper type.
+ ///
+ ///
+ ///
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ // Only take a lock and dispose the image if the method was called explicitly from user code. If instead it was
+ // called from the finalizer, we can make no guarantees on the order of objects being collected, meaning that
+ // the lock itself might have already been collected by the time this code runs, which would lead the lock
+ // statement to throw a NullReferenceException. So if a finalizer is running, just let objects be collected
+ // on their own. This is fine here since there are no unmanaged references to free, but just managed wrappers.
+ lock (this.lockObject)
+ {
+ this.canvasImage?.Dispose();
+ this.canvasImage = null;
+ }
+ }
+
+ this.isDisposed = true;
+ }
+
///
/// Creates the resulting representing the output node of the effect graph for this instance.
///