From a6ff42ee6facfce3ffdae75067c7d4e628b14c81 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Fri, 4 Jun 2021 15:49:52 +1000 Subject: [PATCH] Core - IAudioHandler now implements IDisposable IAudioHandler.Dispose is called when the ChromiumWebBrowser instance is being Disposed. This is a breaking change. Resolves #3598 --- CefSharp.OffScreen/ChromiumWebBrowser.cs | 2 +- CefSharp.WinForms/ChromiumWebBrowser.cs | 2 +- CefSharp.Wpf/ChromiumWebBrowser.cs | 2 +- CefSharp/Handler/AudioHandler.cs | 27 +++++++++++++++++++ CefSharp/Handler/IAudioHandler.cs | 2 +- .../Partial/ChromiumWebBrowser.Partial.cs | 7 ++++- 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 598e9c653..1041e43f0 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -275,7 +275,7 @@ namespace CefSharp.OffScreen // Release reference to handlers, except LifeSpanHandler which is done after Disposing // ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked. - this.SetHandlersToNullExceptLifeSpan(); + FreeHandlersExceptLifeSpan(); browser = null; diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 7762a4e4c..3c4758a31 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -407,7 +407,7 @@ namespace CefSharp.WinForms // Release reference to handlers, except LifeSpanHandler which is done after Disposing // ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked. - this.SetHandlersToNullExceptLifeSpan(); + FreeHandlersExceptLifeSpan(); browser = null; diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 041d21d83..df09d2e6a 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -637,7 +637,7 @@ namespace CefSharp.Wpf // Release reference to handlers, except LifeSpanHandler which is done after Disposing // ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked. - this.SetHandlersToNullExceptLifeSpan(); + this.FreeHandlersExceptLifeSpan(); browser = null; diff --git a/CefSharp/Handler/AudioHandler.cs b/CefSharp/Handler/AudioHandler.cs index 06be75e4f..69f66191e 100644 --- a/CefSharp/Handler/AudioHandler.cs +++ b/CefSharp/Handler/AudioHandler.cs @@ -13,6 +13,17 @@ namespace CefSharp.Handler /// public class AudioHandler : IAudioHandler { + private bool isDisposed; + + /// + /// Gets a value indicating this instance + /// has been disposed. + /// + public bool IsDisposed + { + get { return isDisposed; } + } + /// bool IAudioHandler.GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters) { @@ -117,5 +128,21 @@ namespace CefSharp.Handler { } + + /// + /// Releases unmanaged and managed resources + /// + /// to release both managed and unmanaged resources; to release only unmanaged resources. + protected virtual void Dispose(bool disposing) + { + isDisposed = true; + } + + /// + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } } diff --git a/CefSharp/Handler/IAudioHandler.cs b/CefSharp/Handler/IAudioHandler.cs index 12722cecf..2503a7c24 100644 --- a/CefSharp/Handler/IAudioHandler.cs +++ b/CefSharp/Handler/IAudioHandler.cs @@ -11,7 +11,7 @@ namespace CefSharp /// Implement this interface to handle audio events /// All methods will be called on the CEF UI thread /// - public interface IAudioHandler + public interface IAudioHandler : IDisposable { /// /// Called on the CEF UI thread to allow configuration of audio stream parameters. diff --git a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs index ab5293ee2..f29fa2bca 100644 --- a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs +++ b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs @@ -326,8 +326,13 @@ namespace CefSharp.WinForms partial void OnAfterBrowserCreated(IBrowser browser); - private void SetHandlersToNullExceptLifeSpan() + /// + /// Sets the handler references to null. + /// Where required also calls Dispose(). + /// + private void FreeHandlersExceptLifeSpan() { + AudioHandler?.Dispose(); AudioHandler = null; DialogHandler = null; FindHandler = null;