diff --git a/ipc/mscom/MainThreadHandoff.cpp b/ipc/mscom/MainThreadHandoff.cpp index 521bf7f8e8df..7f9e44a90437 100644 --- a/ipc/mscom/MainThreadHandoff.cpp +++ b/ipc/mscom/MainThreadHandoff.cpp @@ -13,6 +13,7 @@ #include "mozilla/Assertions.h" #include "mozilla/DebugOnly.h" #include "nsThreadUtils.h" +#include "nsProxyRelease.h" using mozilla::DebugOnly; @@ -107,12 +108,11 @@ MainThreadHandoff::Release() if (NS_IsMainThread()) { delete this; } else { - mozilla::DebugOnly 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 self = this; + NS_ReleaseOnMainThread(self.forget()); } } return newRefCnt; diff --git a/ipc/mscom/WeakRef.cpp b/ipc/mscom/WeakRef.cpp index 807fcfb5eea1..80b2e4d633d9 100644 --- a/ipc/mscom/WeakRef.cpp +++ b/ipc/mscom/WeakRef.cpp @@ -11,6 +11,7 @@ #include "mozilla/Mutex.h" #include "nsThreadUtils.h" #include "nsWindowsHelpers.h" +#include "nsProxyRelease.h" static void InitializeCS(CRITICAL_SECTION& aCS) @@ -147,14 +148,11 @@ WeakReferenceSupport::Release() 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 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 self = this; + NS_ReleaseOnMainThread(self.forget()); } } return newRefCnt;