Run callback of setDestructor immediately when GC happens

Fix #4733.
This commit is contained in:
Cheng Zhao 2016-03-21 21:42:12 +09:00
parent a52285596e
commit 52d09e9600
2 changed files with 9 additions and 9 deletions

View file

@ -31,16 +31,16 @@ ObjectLifeMonitor::ObjectLifeMonitor(v8::Isolate* isolate,
// static // static
void ObjectLifeMonitor::OnObjectGC( void ObjectLifeMonitor::OnObjectGC(
const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) { const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
// Usually FirstWeakCallback should do nothing other than reset |object_|
// and then set a second weak callback to run later. We can sidestep that,
// because posting a task to the current message loop is all but free - but
// DO NOT add any more work to this method. The only acceptable place to add
// code is RunCallback.
ObjectLifeMonitor* self = data.GetParameter(); ObjectLifeMonitor* self = data.GetParameter();
self->target_.Reset(); self->target_.Reset();
base::MessageLoop::current()->PostTask( self->RunCallback();
FROM_HERE, base::Bind(&ObjectLifeMonitor::RunCallback, data.SetSecondPassCallback(Free);
self->weak_ptr_factory_.GetWeakPtr())); }
// static
void ObjectLifeMonitor::Free(
const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
delete data.GetParameter();
} }
void ObjectLifeMonitor::RunCallback() { void ObjectLifeMonitor::RunCallback() {
@ -50,7 +50,6 @@ void ObjectLifeMonitor::RunCallback() {
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
v8::Local<v8::Function>::New(isolate_, destructor_)->Call( v8::Local<v8::Function>::New(isolate_, destructor_)->Call(
context->Global(), 0, nullptr); context->Global(), 0, nullptr);
delete this;
} }
} // namespace atom } // namespace atom

View file

@ -23,6 +23,7 @@ class ObjectLifeMonitor {
v8::Local<v8::Function> destructor); v8::Local<v8::Function> destructor);
static void OnObjectGC(const v8::WeakCallbackInfo<ObjectLifeMonitor>& data); static void OnObjectGC(const v8::WeakCallbackInfo<ObjectLifeMonitor>& data);
static void Free(const v8::WeakCallbackInfo<ObjectLifeMonitor>& data);
void RunCallback(); void RunCallback();