Bug 1858842 - Fix nsMainThreadPtrHolder's destructor during shutdown. r=xpcom-reviewers,mccr8

As it is written, if the MOZ_ASSERT(mMainThreadEventTarget) fails, we'd
release the object on the current thread, which is not what we want to
do.

NS_ReleaseOnMainThread deals with shutdown leaking the object, which is
safer. Nobody overrides mMainThreadEventTarget so make the code simpler
while at it.

Differential Revision: https://phabricator.services.mozilla.com/D191108
This commit is contained in:
Emilio Cobos Álvarez 2023-10-17 11:23:13 +00:00
Родитель 61151d6225
Коммит 4ddb450803
1 изменённых файлов: 6 добавлений и 16 удалений

Просмотреть файл

@ -233,11 +233,9 @@ class MOZ_IS_SMARTPTR_TO_REFCOUNTED nsMainThreadPtrHolder final {
// off-main-thread. But some consumers need to use the same pointer for
// multiple classes, some of which are main-thread-only and some of which
// aren't. So we allow them to explicitly disable this strict checking.
nsMainThreadPtrHolder(const char* aName, T* aPtr, bool aStrict = true,
nsIEventTarget* aMainThreadEventTarget = nullptr)
nsMainThreadPtrHolder(const char* aName, T* aPtr, bool aStrict = true)
: mRawPtr(aPtr),
mStrict(aStrict),
mMainThreadEventTarget(aMainThreadEventTarget)
mStrict(aStrict)
#ifndef RELEASE_OR_BETA
,
mName(aName)
@ -249,11 +247,9 @@ class MOZ_IS_SMARTPTR_TO_REFCOUNTED nsMainThreadPtrHolder final {
NS_IF_ADDREF(mRawPtr);
}
nsMainThreadPtrHolder(const char* aName, already_AddRefed<T> aPtr,
bool aStrict = true,
nsIEventTarget* aMainThreadEventTarget = nullptr)
bool aStrict = true)
: mRawPtr(aPtr.take()),
mStrict(aStrict),
mMainThreadEventTarget(aMainThreadEventTarget)
mStrict(aStrict)
#ifndef RELEASE_OR_BETA
,
mName(aName)
@ -274,17 +270,13 @@ class MOZ_IS_SMARTPTR_TO_REFCOUNTED nsMainThreadPtrHolder final {
if (NS_IsMainThread()) {
NS_IF_RELEASE(mRawPtr);
} else if (mRawPtr) {
if (!mMainThreadEventTarget) {
mMainThreadEventTarget = do_GetMainThread();
}
MOZ_ASSERT(mMainThreadEventTarget);
NS_ProxyRelease(
NS_ReleaseOnMainThread(
#ifdef RELEASE_OR_BETA
nullptr,
#else
mName,
#endif
mMainThreadEventTarget, dont_AddRef(mRawPtr));
dont_AddRef(mRawPtr));
}
}
@ -312,8 +304,6 @@ class MOZ_IS_SMARTPTR_TO_REFCOUNTED nsMainThreadPtrHolder final {
// Whether to strictly enforce thread invariants in this class.
bool mStrict = true;
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
#ifndef RELEASE_OR_BETA
const char* mName = nullptr;
#endif