Skip to content

Commit

Permalink
fix: crash in BrowserWindow destructor after win.webContents.destroy() (
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored and zcbenz committed Jun 15, 2019
1 parent 972d45d commit 2dd2566
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions atom/browser/api/atom_api_browser_window.cc
Expand Up @@ -66,7 +66,7 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
}

web_contents_.Reset(isolate, web_contents.ToV8());
api_web_contents_ = web_contents.get();
api_web_contents_ = web_contents->GetWeakPtr();
api_web_contents_->AddObserver(this);
Observe(api_web_contents_->web_contents());

Expand Down Expand Up @@ -97,7 +97,9 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
}

BrowserWindow::~BrowserWindow() {
api_web_contents_->RemoveObserver(this);
// FIXME This is a hack rather than a proper fix preventing shutdown crashes.
if (api_web_contents_)
api_web_contents_->RemoveObserver(this);
// Note that the OnWindowClosed will not be called after the destructor runs,
// since the window object is managed by the TopLevelWindow class.
if (web_contents())
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_browser_window.h
Expand Up @@ -113,7 +113,7 @@ class BrowserWindow : public TopLevelWindow,
#endif

v8::Global<v8::Value> web_contents_;
api::WebContents* api_web_contents_;
base::WeakPtr<api::WebContents> api_web_contents_;

base::WeakPtrFactory<BrowserWindow> weak_factory_;

Expand Down
8 changes: 5 additions & 3 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -289,7 +289,9 @@ struct WebContents::FrameDispatchHelper {
WebContents::WebContents(v8::Isolate* isolate,
content::WebContents* web_contents,
Type type)
: content::WebContentsObserver(web_contents), type_(type) {
: content::WebContentsObserver(web_contents),
type_(type),
weak_factory_(this) {
const mate::Dictionary options = mate::Dictionary::CreateEmpty(isolate);
if (type == REMOTE) {
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent(),
Expand All @@ -304,8 +306,8 @@ WebContents::WebContents(v8::Isolate* isolate,
}
}

WebContents::WebContents(v8::Isolate* isolate,
const mate::Dictionary& options) {
WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options)
: weak_factory_(this) {
// Read options.
options.Get("backgroundThrottling", &background_throttling_);

Expand Down
4 changes: 4 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -99,6 +99,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

base::WeakPtr<WebContents> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }

// Destroy the managed content::WebContents instance.
//
// Note: The |async| should only be |true| when users are expecting to use the
Expand Down Expand Up @@ -515,6 +517,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Observers of this WebContents.
base::ObserverList<ExtendedWebContentsObserver> observers_;

base::WeakPtrFactory<WebContents> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(WebContents);
};

Expand Down

0 comments on commit 2dd2566

Please sign in to comment.