Skip to content

Commit

Permalink
implement dispose and use repo in cef for emergency dispose on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
JanEggers committed May 17, 2014
1 parent 4a23154 commit 7ccc5a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CefSharp.WinForms/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ private static void OnApplicationExit(object sender, EventArgs e)

public WebView(string address)
{
Cef.AddDisposable(this);
Address = address;
}

protected override void Dispose(bool disposing)
{
Cef.RemoveDisposable(this);

if (disposing)
{
if (managedCefBrowserAdapter != null)
Expand Down
37 changes: 17 additions & 20 deletions CefSharp.Wpf/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class WebView : ContentControl, IRenderWebBrowser, IWpfWebBrowser
private Popup popup;
private ScaleTransform dpiTransform;
private readonly List<IDisposable> disposables = new List<IDisposable>();
private static readonly List<WebView> webViews = new List<WebView>();

public BrowserSettings BrowserSettings { get; set; }
public bool IsBrowserInitialized { get; private set; }
Expand Down Expand Up @@ -258,17 +257,22 @@ protected virtual void OnCleanupElementChanged(FrameworkElement oldValue, Framew

private void OnCleanupElementUnloaded(object sender, RoutedEventArgs e)
{
Cleanup();
Dispose();
}

protected virtual void Cleanup()
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool isdisposing)
{
Cef.RemoveDisposable(this);
foreach (var disposable in disposables)
{
disposable.Dispose();
}

webViews.Remove(this);
}

#endregion CleanupElement dependency property
Expand Down Expand Up @@ -320,7 +324,7 @@ static WebView()

public WebView()
{
webViews.Add(this);
Cef.AddDisposable(this);
Focusable = true;
FocusVisualStyle = null;
IsTabStop = true;
Expand Down Expand Up @@ -349,7 +353,7 @@ public WebView()
ZoomOutCommand = new DelegateCommand(ZoomOut);
ZoomResetCommand = new DelegateCommand(ZoomReset);
ViewSourceCommand = new DelegateCommand(ViewSource);
CleanupCommand = new DelegateCommand(Cleanup);
CleanupCommand = new DelegateCommand(Dispose);

managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this);
managedCefBrowserAdapter.CreateOffscreenBrowser(BrowserSettings ?? new BrowserSettings());
Expand All @@ -360,6 +364,11 @@ public WebView()
disposables.Add(new DisposableEventWrapper(this, ActualWidthProperty, OnActualSizeChanged));
}

~WebView()
{
Dispose(false);
}

private class DisposableEventWrapper : IDisposable
{
public DependencyObject Source { get; private set; }
Expand Down Expand Up @@ -407,19 +416,7 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg

private static void OnApplicationExit(object sender, ExitEventArgs e)
{
// TODO: This prevents AccessViolation on shutdown, but it would be better handled by the Cef class; the WebView
// control should not explicitly have to perform this.
if (Cef.IsInitialized)
{
foreach (var webView in webViews.ToList())
{
webView.Cleanup();
}
GC.Collect();
GC.WaitForPendingFinalizers();

Cef.Shutdown();
}
Cef.Shutdown();
}

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
Expand Down
2 changes: 1 addition & 1 deletion CefSharp/IWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace CefSharp
/// <param name="errorText">The error text.</param>
public delegate void LoadErrorEventHandler(string failedUrl, CefErrorCode errorCode, string errorText);

public interface IWebBrowser
public interface IWebBrowser : IDisposable
{
/// <summary>
/// Event handler for receiving Javascript console messages being sent from web pages.
Expand Down

0 comments on commit 7ccc5a5

Please sign in to comment.