Bug 1336510 - Part 7: Stop capturing this by value in windows-only code, r=aklotz, a=abillings
authorMichael Layzell <michael@thelayzells.com>
Thu, 16 Feb 2017 14:38:09 -0500
changeset 376474 98f5538946ebadf23fe2b26724b4e7810e1ca137
parent 376473 5022715a5ef312be685fee827ae3ee3e03f4b59d
child 376475 174a2eb64874b418276b74e5098c3179c5dbc95a
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz, abillings
bugs1336510
milestone53.0a2
Bug 1336510 - Part 7: Stop capturing this by value in windows-only code, r=aklotz, a=abillings 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:
@@ -105,22 +106,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
@@ -7,16 +7,17 @@
 #define INITGUID
 #include "mozilla/mscom/WeakRef.h"
 
 #include "mozilla/Assertions.h"
 #include "mozilla/DebugOnly.h"
 #include "mozilla/RefPtr.h"
 #include "nsThreadUtils.h"
 #include "nsWindowsHelpers.h"
+#include "nsProxyRelease.h"
 
 namespace mozilla {
 namespace mscom {
 
 WeakReferenceSupport::WeakReferenceSupport(Flags aFlags)
   : mRefCnt(1)
   , mFlags(aFlags)
 {
@@ -76,24 +77,21 @@ WeakReferenceSupport::Release()
     if (newRefCnt == 0) {
       ClearWeakRefs();
     }
   }
   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;
 }
 
 void
 WeakReferenceSupport::ClearWeakRefs()
 {