From 968f75529aad8e9198a8872e5740c943757cf614 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 8 Jan 2014 11:55:54 +0800 Subject: [PATCH] Fix possible crashes when quiting message loop. --- common/node_bindings.cc | 6 +++--- common/node_bindings.h | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/common/node_bindings.cc b/common/node_bindings.cc index 317f679599dc..5bd69a680d9a 100644 --- a/common/node_bindings.cc +++ b/common/node_bindings.cc @@ -65,7 +65,8 @@ NodeBindings::NodeBindings(bool is_browser) : is_browser_(is_browser), message_loop_(NULL), uv_loop_(uv_default_loop()), - embed_closed_(false) { + embed_closed_(false), + weak_factory_(this) { } NodeBindings::~NodeBindings() { @@ -76,7 +77,6 @@ NodeBindings::~NodeBindings() { // Wait for everything to be done. uv_thread_join(&embed_thread_); - message_loop_->RunUntilIdle(); // Clear uv. uv_sem_destroy(&embed_sem_); @@ -209,7 +209,7 @@ void NodeBindings::UvRunOnce() { void NodeBindings::WakeupMainThread() { DCHECK(message_loop_); message_loop_->PostTask(FROM_HERE, base::Bind(&NodeBindings::UvRunOnce, - base::Unretained(this))); + weak_factory_.GetWeakPtr())); } void NodeBindings::WakeupEmbedThread() { diff --git a/common/node_bindings.h b/common/node_bindings.h index 7ed22806d115..f777c220afc1 100644 --- a/common/node_bindings.h +++ b/common/node_bindings.h @@ -6,6 +6,7 @@ #define ATOM_COMMON_NODE_BINDINGS_H_ #include "base/basictypes.h" +#include "base/memory/weak_ptr.h" #include "v8/include/v8.h" #include "vendor/node/deps/uv/include/uv.h" @@ -83,6 +84,8 @@ class NodeBindings { // Semaphore to wait for main loop in the embed thread. uv_sem_t embed_sem_; + base::WeakPtrFactory weak_factory_; + DISALLOW_COPY_AND_ASSIGN(NodeBindings); };