From c577733ac794ac53a5f242a2b6048961f3b5e568 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 3 Aug 2022 22:59:24 +0200 Subject: [PATCH] Only get reference tracking leases when rendering --- .../Helpers/SwapChainManager.Rendering.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) 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;