diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index 6dbb2ec5..893d04a4 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -305,10 +305,7 @@ partial struct PixelShaderEffect @this = (PixelShaderEffect*)&((void**)@this)[-1]; // Free the previous ID2D1DrawInfo object, if present - if (@this->d2D1DrawInfo is not null) - { - _ = @this->d2D1DrawInfo->Release(); - } + ComPtr.Release(@this->d2D1DrawInfo); // Store the new ID2D1DrawInfo object _ = drawInfo->AddRef(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index 6c70ad3d..5f1d26bf 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -116,10 +116,7 @@ internal unsafe partial struct PixelShaderEffect if (Windows.SUCCEEDED(hresult)) { // Free the previous ID2D1EffectContext object, if present - if (@this->d2D1EffectContext is not null) - { - _ = @this->d2D1EffectContext->Release(); - } + ComPtr.Release(@this->d2D1EffectContext); // Store the new ID2D1EffectContext object _ = effectContext->AddRef(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 9abc2a11..25dc7e52 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -263,10 +263,7 @@ unsafe partial struct PixelShaderEffect } // If there's already an existing manager, release it - if (@this->d2D1TransformMapper is not null) - { - _ = ((IUnknown*)@this->d2D1TransformMapper)->Release(); - } + ComPtr.Release(@this->d2D1TransformMapper); // Store the transform mapper manager into the effect @this->d2D1TransformMapper = transformMapper.Detach(); @@ -389,10 +386,7 @@ unsafe partial struct PixelShaderEffect ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index]; // If there's already an existing manager at this index, release it - if (currentResourceTextureManager is not null) - { - _ = ((IUnknown*)currentResourceTextureManager)->Release(); - } + ComPtr.Release(currentResourceTextureManager); // Store the resource texture manager into the buffer currentResourceTextureManager = resourceTextureManager.Detach(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 49f7428b..8aa37e86 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -243,30 +243,14 @@ internal unsafe partial struct PixelShaderEffect NativeMemory.Free(this.constantBuffer); - if (this.d2D1TransformMapper is not null) - { - _ = ((IUnknown*)this.d2D1TransformMapper)->Release(); - } - - if (this.d2D1DrawInfo is not null) - { - _ = this.d2D1DrawInfo->Release(); - } - - if (this.d2D1EffectContext is not null) - { - _ = this.d2D1EffectContext->Release(); - } + ComPtr.Release(this.d2D1TransformMapper); + ComPtr.Release(this.d2D1DrawInfo); + ComPtr.Release(this.d2D1EffectContext); // Retrieve all possible resource texture managers in use and release the ones that had been assigned (from one of the property bindings) for (int i = 0; i < resourceTextureDescriptionCount; i++) { - ID2D1ResourceTextureManager* resourceTextureManager = this.resourceTextureManagerBuffer[i]; - - if (resourceTextureManager is not null) - { - _ = ((IUnknown*)resourceTextureManager)->Release(); - } + ComPtr.Release(this.resourceTextureManagerBuffer[i]); } NativeMemory.Free(Unsafe.AsPointer(ref this)); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index e501fddb..a5fe07c4 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -247,10 +247,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl this.d2D1Multithread->Enter(); // Release the resource too if it has been created - if (this.d2D1ResourceTexture is not null) - { - _ = this.d2D1ResourceTexture->Release(); - } + ComPtr.Release(this.d2D1ResourceTexture); // Now that the resource, if any, has been released, the effect context can also // be released. This must be done only after any associated resource textures have diff --git a/src/TerraFX.Interop.Windows/ComPtr`1.cs b/src/TerraFX.Interop.Windows/ComPtr`1.cs index dcfa9e1f..abb74bcc 100644 --- a/src/TerraFX.Interop.Windows/ComPtr`1.cs +++ b/src/TerraFX.Interop.Windows/ComPtr`1.cs @@ -38,6 +38,19 @@ internal unsafe struct ComPtr : IDisposable InternalAddRef(); } + /// + /// Releases a target COM object, if it's not . + /// + /// The COM object to potentially release. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Release(T* other) + { + if (other is not null) + { + _ = ((IUnknown*)other)->Release(); + } + } + /// Converts a raw pointer to a new instance and increments the ref count. /// The raw pointer to wrap. [MethodImpl(MethodImplOptions.AggressiveInlining)]