[xpcom] Make sure the method survives the duration of an async call
authorKalle Vahlman <kalle.vahlman@movial.com>
Mon, 15 Jun 2009 15:26:28 +0000 (18:26 +0300)
committerKalle Vahlman <kalle.vahlman@movial.com>
Tue, 16 Jun 2009 06:32:54 +0000 (09:32 +0300)
xpcom-dbusservice/DBusMethod.cpp

index cde68dd..3afb3e5 100644 (file)
@@ -156,11 +156,13 @@ static void
 ReplyHandler(DBusPendingCall *pending, void *user_data)
 {
     DBusMessage *reply;
-    nsCOMPtr<DBusMethod> method = (DBusMethod *) user_data;
+    DBusMethod *method = (DBusMethod *) user_data;
 
     reply = dbus_pending_call_steal_reply(pending);
     DoCallBack(method, reply);
     dbus_message_unref(reply);
+    // We took a ref when starting the async call, release it here
+    method->Release();
 }
 
 
@@ -317,12 +319,16 @@ DBusMethod::DoCall(nsIVariant **aArgs, PRUint32 aCount)
     {
         if (mAsync)
         {
-            /* FIXME - do we need to AddRef "this" */
             BDBLOG(("  do async reply callback\n"));
-            dbus_pending_call_set_notify(pending,
-                                         ReplyHandler,
-                                         this,
-                                         nsnull);
+            if (dbus_pending_call_set_notify(pending,
+                                             ReplyHandler,
+                                             this,
+                                             nsnull))
+            {
+                // Add a ref to make sure the method object doesn't die
+                // before the reply comes.
+                this->AddRef();
+            }
         }
         else
         {