Bug 1336510 - Part 7: Stop capturing this by value in windows-only code, r=aklotz
authorMichael Layzell <michael@thelayzells.com>
Thu, 16 Feb 2017 14:38:09 -0500
changeset 373843 a545bcb544a738353f521593643f8ed3c5924c5b
parent 373842 b4cb7a1e84a85fe979bec7263c1a7c71a4a5323c
child 373844 62ea86b5a4e27f2a96a3f8324b7be83955431035
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1336510
milestone54.0a1
Bug 1336510 - Part 7: Stop capturing this by value in windows-only code, r=aklotz MozReview-Commit-ID: B4dYo4ETzkL
ipc/mscom/MainThreadHandoff.cpp
ipc/mscom/WeakRef.cpp
--- a/ipc/mscom/MainThreadHandoff.cpp
+++ b/ipc/mscom/MainThreadHandoff.cpp
@@ -8,16 +8,17 @@
 
 #include "mozilla/Move.h"
 #include "mozilla/mscom/InterceptorLog.h"
 #include "mozilla/mscom/Registration.h"
 #include "mozilla/mscom/Utils.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/DebugOnly.h"
 #include "nsThreadUtils.h"
+#include "nsProxyRelease.h"
 
 using mozilla::DebugOnly;
 
 namespace {
 
 class HandoffRunnable : public mozilla::Runnable
 {
 public:
@@ -102,22 +103,21 @@ MainThreadHandoff::Release()
 {
   ULONG newRefCnt = (ULONG) InterlockedDecrement((LONG*)&mRefCnt);
   if (newRefCnt == 0) {
     // It is possible for the last Release() call to happen off-main-thread.
     // If so, we need to dispatch an event to delete ourselves.
     if (NS_IsMainThread()) {
       delete this;
     } else {
-      mozilla::DebugOnly<nsresult> rv =
-        NS_DispatchToMainThread(NS_NewRunnableFunction([=]() -> void
-        {
-          delete this;
-        }));
-      MOZ_ASSERT(NS_SUCCEEDED(rv));
+      // We need to delete this object on the main thread, but we aren't on the
+      // main thread right now, so we send a reference to ourselves to the main
+      // thread to be re-released there.
+      RefPtr<MainThreadHandoff> self = this;
+      NS_ReleaseOnMainThread(self.forget());
     }
   }
   return newRefCnt;
 }
 
 HRESULT
 MainThreadHandoff::OnCall(ICallFrame* aFrame)
 {
--- a/ipc/mscom/WeakRef.cpp
+++ b/ipc/mscom/WeakRef.cpp
@@ -6,16 +6,17 @@
 
 #define INITGUID
 #include "mozilla/mscom/WeakRef.h"
 
 #include "mozilla/DebugOnly.h"
 #include "mozilla/Mutex.h"
 #include "nsThreadUtils.h"
 #include "nsWindowsHelpers.h"
+#include "nsProxyRelease.h"
 
 static void
 InitializeCS(CRITICAL_SECTION& aCS)
 {
   DWORD flags = 0;
 #if defined(RELEASE_OR_BETA)
   flags |= CRITICAL_SECTION_NO_DEBUG_INFO;
 #endif
@@ -142,24 +143,21 @@ WeakReferenceSupport::Release()
       mSharedRef->Clear();
     }
   }
   NS_LOG_RELEASE(this, newRefCnt, "mscom::WeakReferenceSupport");
   if (newRefCnt == 0) {
     if (mFlags != Flags::eDestroyOnMainThread || NS_IsMainThread()) {
       delete this;
     } else {
-      // It is possible for the last Release() call to happen off-main-thread.
-      // If so, we need to dispatch an event to delete ourselves.
-      mozilla::DebugOnly<nsresult> rv =
-        NS_DispatchToMainThread(NS_NewRunnableFunction([this]() -> void
-        {
-          delete this;
-        }));
-      MOZ_ASSERT(NS_SUCCEEDED(rv));
+      // We need to delete this object on the main thread, but we aren't on the
+      // main thread right now, so we send a reference to ourselves to the main
+      // thread to be re-released there.
+      RefPtr<WeakReferenceSupport> self = this;
+      NS_ReleaseOnMainThread(self.forget());
     }
   }
   return newRefCnt;
 }
 
 HRESULT
 WeakReferenceSupport::GetWeakReference(IWeakReference** aOutWeakRef)
 {