diff --git a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs
index 9c3710ae..e940962a 100644
--- a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs
+++ b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs
@@ -90,6 +90,12 @@ public sealed partial class AnimatedComputeShaderPanel : SwapChainPanel, IDispos
///
public void Dispose()
{
+ // Also request the render loop to stop before disposing. This ensures that in case there's
+ // a render thread already ongoing that is using a custom frame request queue, it might have
+ // the ability to stop rendering immediately and safely, instead of causing the panel to remain
+ // alive and then just throwing an exception the next time a frame is requested in case the
+ // device ot other resources have also been disposed.
+ this.swapChainManager.StopRenderLoop();
this.swapChainManager.Dispose();
}
}
diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs
index aa6860b0..1e4f6328 100644
--- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs
+++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs
@@ -29,10 +29,13 @@ partial class SwapChainManager
{
Guard.IsNotNull(shaderRunner);
- using var _0 = GetReferenceTrackingLease();
-
using (await this.setupSemaphore.LockAsync())
{
+ // Here and in the calls below, only get a reference tracking lease when starting a render thread.
+ // This avoids crashes when the panel has been disposed but some code (eg. event handlers) still
+ // ends up calling into any of the APIs in this file, which is not intended but should not crash.
+ using var _0 = GetReferenceTrackingLease();
+
this.renderCancellationTokenSource?.Cancel();
await this.renderSemaphore.WaitAsync();
@@ -71,13 +74,13 @@ partial class SwapChainManager
/// Whether or not to use dynamic resolution.
public async void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled)
{
- using var _0 = GetReferenceTrackingLease();
-
using (await this.setupSemaphore.LockAsync())
{
// If there is a render thread currently running, stop it and restart it
if (this.renderCancellationTokenSource?.IsCancellationRequested == false)
{
+ using var _0 = GetReferenceTrackingLease();
+
this.renderCancellationTokenSource?.Cancel();
await this.renderSemaphore.WaitAsync();
@@ -105,14 +108,14 @@ partial class SwapChainManager
/// Whether or not to use vertical sync.
public async void QueueVerticalSyncModeChange(bool isVerticalSyncEnabled)
{
- using var _0 = GetReferenceTrackingLease();
-
using (await this.setupSemaphore.LockAsync())
{
// The v-sync option can be toggled on the fly when not using dynamic resolution
if (this.renderCancellationTokenSource?.IsCancellationRequested == false &&
this.isDynamicResolutionEnabled)
{
+ using var _0 = GetReferenceTrackingLease();
+
this.renderCancellationTokenSource?.Cancel();
await this.renderSemaphore.WaitAsync();
@@ -140,8 +143,6 @@ partial class SwapChainManager
/// The height of the render resolution.
public void QueueResize(double width, double height)
{
- using var _0 = GetReferenceTrackingLease();
-
this.width = (float)width;
this.height = (float)height;
@@ -155,8 +156,6 @@ partial class SwapChainManager
/// The composition scale on the Y axis
public void QueueCompositionScaleChange(double compositionScaleX, double compositionScaleY)
{
- using var _0 = GetReferenceTrackingLease();
-
this.compositionScaleX = (float)compositionScaleX;
this.compositionScaleY = (float)compositionScaleY;
@@ -169,8 +168,6 @@ partial class SwapChainManager
/// The resolution scale factor to use.
public void QueueResolutionScaleChange(double resolutionScale)
{
- using var _0 = GetReferenceTrackingLease();
-
this.resolutionScale = (float)resolutionScale;
this.isResizePending = true;
diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs
index 53f6af10..ea9edbc0 100644
--- a/src/ComputeSharp/Graphics/GraphicsDevice.cs
+++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs
@@ -118,7 +118,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
/// when the device is collected. But, due to the fact that could happen at any time, a list is required to guarantee
/// the additional references added by the native objects in the pipeline model can be released immediately.
///
- private List cachedPipelineData;
+ private readonly List cachedPipelineData;
///
/// Raised whenever the device is lost.