From 919dd1591423870500e51e573ca312174adfeda5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Jul 2021 15:59:01 +0200 Subject: [PATCH] Fixed memory leak and duplicate initialization in WinUI panel --- .../Views/MainWindow.xaml | 6 +-- .../ComputeShaderPanel.Rendering.cs | 44 +++++++++++++++++-- src/ComputeSharp.WinUI/ComputeShaderPanel.cs | 8 ++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml index 783667ec..5ab52fd5 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml @@ -107,9 +107,9 @@ ShaderRunner="{x:Bind ShaderRunner}"/> + current template scope. Once that's fixed in WinUI 3, this can be rewritten as a multi-binding like this: + Visibility="{x:Bind system:Object.ReferenceEquals((local:ComputeShader), ViewModel.SelectedComputeShader), Mode=OneWay}". + When that is working, the ComputeShader.IsSelected property can also be removed, as it will no longer be needed.--> /// Indicates whether or not the rendering has been canceled. /// - private bool isCancellationRequested; + private volatile bool isCancellationRequested; /// /// The instance tracking time since the first rendered frame. /// private Stopwatch? renderStopwatch; + /// + /// Indicates whether or not has already been called. + /// + private bool isInitialized; + /// /// Initializes the current application. /// private unsafe void OnInitialize() { + if (isInitialized) + { + return; + } + + isInitialized = true; + // Get the underlying ID3D12Device in use fixed (ID3D12Device** d3D12Device = this.d3D12Device) { @@ -160,10 +172,8 @@ namespace ComputeSharp.WinUI } // Create the swap chain to display frames - using (ComPtr dxgiSwapChain1 = default) + using (ComPtr dxgiFactory2 = default) { - using ComPtr dxgiFactory2 = default; - FX.CreateDXGIFactory2(FX.DXGI_CREATE_FACTORY_DEBUG, FX.__uuidof(), (void**)dxgiFactory2.GetAddressOf()).Assert(); DXGI_SWAP_CHAIN_DESC1 dxgiSwapChainDesc1 = default; @@ -179,6 +189,8 @@ namespace ComputeSharp.WinUI dxgiSwapChainDesc1.SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; dxgiSwapChainDesc1.BufferUsage = FX.DXGI_USAGE_RENDER_TARGET_OUTPUT; + using ComPtr dxgiSwapChain1 = default; + dxgiFactory2.Get()->CreateSwapChainForComposition( (IUnknown*)this.d3D12CommandQueue.Get(), &dxgiSwapChainDesc1, @@ -442,5 +454,29 @@ namespace ComputeSharp.WinUI { this.isCancellationRequested = true; } + + /// + /// Stops the current render loop, and releases all resources. + /// + private void OnDisposed() + { + this.isCancellationRequested = true; + + // Ensure the rendering has stopped + if (this.renderThread is Thread renderThread) + { + renderThread.Join(); + } + + this.d3D12Device.Dispose(); + this.d3D12CommandQueue.Dispose(); + this.d3D12Fence.Dispose(); + this.d3D12CommandAllocator.Dispose(); + this.d3D12GraphicsCommandList.Dispose(); + this.dxgiSwapChain2.Dispose(); + this.d3D12Resource0.Dispose(); + this.d3D12Resource1.Dispose(); + this.texture?.Dispose(); + } } } diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs index dbbf41da..06bf076c 100644 --- a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs +++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs @@ -20,6 +20,14 @@ namespace ComputeSharp.WinUI this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged; } + /// + /// Finalizes the current instance and releases all resources. + /// + ~ComputeShaderPanel() + { + OnDisposed(); + } + // Initializes the swap chain and starts the render thread private void ComputeShaderPanel_Loaded(object sender, RoutedEventArgs e) {