From fc6afdbff4a04dacbfbd4945971c3d256d72b300 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 19 Aug 2022 16:52:47 +0200 Subject: [PATCH] Fix stack overflow when releasing resource texture managers --- .../ResourceManagers/D2D1ResourceTextureManagerImpl.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index a10c9b3f..cf7f0cfc 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -234,7 +234,6 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl // 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(); - this.d2D1EffectContext->Release(); // Release the resource too if it has been created if (this.d2D1ResourceTexture is not null) @@ -242,6 +241,12 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl this.d2D1ResourceTexture->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(); }