Run callback of setDestructor immediately when GC happens
authorCheng Zhao <zcbenz@gmail.com>
Mon, 21 Mar 2016 12:42:12 +0000 (21:42 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 21 Mar 2016 12:42:12 +0000 (21:42 +0900)
Fix #4733.

atom/common/api/object_life_monitor.cc
atom/common/api/object_life_monitor.h

index 9b7c7fe..916ad8a 100644 (file)
@@ -31,16 +31,16 @@ ObjectLifeMonitor::ObjectLifeMonitor(v8::Isolate* isolate,
 // static
 void ObjectLifeMonitor::OnObjectGC(
     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();
   self->target_.Reset();
-  base::MessageLoop::current()->PostTask(
-      FROM_HERE, base::Bind(&ObjectLifeMonitor::RunCallback,
-                            self->weak_ptr_factory_.GetWeakPtr()));
+  self->RunCallback();
+  data.SetSecondPassCallback(Free);
+}
+
+// static
+void ObjectLifeMonitor::Free(
+    const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
+  delete data.GetParameter();
 }
 
 void ObjectLifeMonitor::RunCallback() {
@@ -50,7 +50,6 @@ void ObjectLifeMonitor::RunCallback() {
   v8::Context::Scope context_scope(context);
   v8::Local<v8::Function>::New(isolate_, destructor_)->Call(
       context->Global(), 0, nullptr);
-  delete this;
 }
 
 }  // namespace atom
index 4c932b9..82d923f 100644 (file)
@@ -23,6 +23,7 @@ class ObjectLifeMonitor {
                     v8::Local<v8::Function> destructor);
 
   static void OnObjectGC(const v8::WeakCallbackInfo<ObjectLifeMonitor>& data);
+  static void Free(const v8::WeakCallbackInfo<ObjectLifeMonitor>& data);
 
   void RunCallback();