diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index c0ac8d04..22887754 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -235,37 +235,38 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl if (referenceCount == 0) { - lock (this.lockHandle.Target!) + // Note: if this object is being destroyed, it means that no other caller can exist. As such, there + // is no need to take the instance lock, as it's impossible for this logic to be racing against other + // threads referencing this object. If someone else was doing this, this would be falling apart anyway. + // As such, we can simply free this GC handle and run the rest of the logic without the additional lock. + this.lockHandle.Free(); + + // Synchronize on the ID2D1Multithread instance before touching D2D objects + if (this.d2D1Multithread is not null) { - // Also synchronize on the ID2D1Multithread instance before touching D2D objects - if (this.d2D1Multithread is not null) - { - // Enter the lock, and then free the effect context. That is guaranteed to - // not be null here, as it is only ever set if ID2D1Multithread is retrieved. - this.d2D1Multithread->Enter(); + // Enter the lock, and then free the effect context. That is guaranteed to + // not be null here, as it is only ever set if ID2D1Multithread is retrieved. + this.d2D1Multithread->Enter(); - // Release the resource too if it has been created - ComPtr.Release(this.d2D1ResourceTexture); + // Release the resource too if it has been created + 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 - // been released. Releasing an effect context first, in case it was the last reference - // to it, will otherwise cause the Release() call on the resource texture to explode. - _ = this.d2D1EffectContext->Release(); + // 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 + // been released. Releasing an effect context first, in case it was the last reference + // to it, will otherwise cause the Release() call on the resource texture to explode. + _ = this.d2D1EffectContext->Release(); - this.d2D1Multithread->Leave(); + this.d2D1Multithread->Leave(); - _ = this.d2D1Multithread->Release(); - } - - NativeMemory.Free(this.resourceId); - NativeMemory.Free(this.resourceTextureProperties.extents); - NativeMemory.Free(this.resourceTextureProperties.extendModes); - NativeMemory.Free(this.data); - NativeMemory.Free(this.strides); + _ = this.d2D1Multithread->Release(); } - this.lockHandle.Free(); + NativeMemory.Free(this.resourceId); + NativeMemory.Free(this.resourceTextureProperties.extents); + NativeMemory.Free(this.resourceTextureProperties.extendModes); + NativeMemory.Free(this.data); + NativeMemory.Free(this.strides); NativeMemory.Free(Unsafe.AsPointer(ref this)); }