Fix possible crashes when quiting message loop.

This commit is contained in:
Cheng Zhao 2014-01-08 11:55:54 +08:00
parent 93d5a2e195
commit 968f75529a
2 changed files with 6 additions and 3 deletions

View file

@ -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() {

View file

@ -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<NodeBindings> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(NodeBindings);
};