Bug 1715026 - Properly transfer ownership of the memory pressure watcher to the memory resource callback. r=tkikuchi, a=jcristau
authorGabriele Svelto <gsvelto@mozilla.com>
Sat, 12 Jun 2021 05:54:01 +0000
changeset 649706 cc5c59a8cfb02014c7af9f6cf586e2cd69aaf0e8
parent 649705 8709da2875edc223f099919bf1ecc6bc7e9a7992
child 649707 52e3ce6ce5beab5a210ac70217d01361e632e4cc
push id15542
push userjcristau@mozilla.com
push dateMon, 14 Jun 2021 16:15:48 +0000
treeherdermozilla-beta@2d1d0a88a86c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstkikuchi, jcristau
bugs1715026
milestone90.0
Bug 1715026 - Properly transfer ownership of the memory pressure watcher to the memory resource callback. r=tkikuchi, a=jcristau Differential Revision: https://phabricator.services.mozilla.com/D117051
xpcom/base/AvailableMemoryTracker.cpp
--- a/xpcom/base/AvailableMemoryTracker.cpp
+++ b/xpcom/base/AvailableMemoryTracker.cpp
@@ -212,25 +212,26 @@ void nsAvailableMemoryWatcher::Shutdown(
     mTimer = nullptr;
   }
 
   UnregisterMemoryResourceHandler();
 }
 
 bool nsAvailableMemoryWatcher::ListenForLowMemory() {
   if (mLowMemoryHandle && !mWaitHandle) {
+    // We're giving ownership of this object to the LowMemoryCallback(). We
+    // increment the count here so that the object is kept alive until the
+    // callback decrements it.
+    this->AddRef();
     bool res = ::RegisterWaitForSingleObject(
         &mWaitHandle, mLowMemoryHandle, LowMemoryCallback, this, INFINITE,
         WT_EXECUTEDEFAULT | WT_EXECUTEONLYONCE);
-    if (res) {
-      // We bump the reference count when registering the callback to keep the
-      // object alive in case of shutdown. Note that the reference count will be
-      // decremented by the callback itself only when it gets called, if it
-      // doesn't we have to decrement it ourselves.
-      this->AddRef();
+    if (!res) {
+      // We couldn't register the callback, decrement the count
+      this->Release();
     }
     return res;
   }
 
   return false;
 }
 
 void nsAvailableMemoryWatcher::OnLowMemory(const MutexAutoLock&) {