Bug 1513542 - Fix static deinit of the nsStandardURL leak debugging helper. r=valentin
☠☠ backed out by 7359f1ce3a65 ☠ ☠
authorDragana Damjanovic <dd.mozilla@gmail.com>
Tue, 18 Dec 2018 23:58:51 +0000
changeset 451262 dcd8195681d70ee47bde2e80846fc62b1e63eb74
parent 451218 8d24aeda8861e73c1491f5c45f4e57c2a77422d5
child 451263 f626107995b58eb5453c30f223753ea2ae31aa8b
push id35232
push userebalazs@mozilla.com
push dateWed, 19 Dec 2018 15:45:00 +0000
treeherdermozilla-central@335cc6f2cbb1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1513542
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1513542 - Fix static deinit of the nsStandardURL leak debugging helper. r=valentin It was crashing in DumpLeakedURLs::~DumpLeakedURLs(). If we have never used DumpLeakedURLs we were initializing gAllURLsMutex in a destructor of another static variable. Differential Revision: https://phabricator.services.mozilla.com/D14263
netwerk/base/nsStandardURL.cpp
--- a/netwerk/base/nsStandardURL.cpp
+++ b/netwerk/base/nsStandardURL.cpp
@@ -186,18 +186,53 @@ const nsACString &nsStandardURL::nsSegme
   return str;
 }
 
 //----------------------------------------------------------------------------
 // nsStandardURL <public>
 //----------------------------------------------------------------------------
 
 #ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
-static StaticMutex gAllURLsMutex;
-static LinkedList<nsStandardURL> gAllURLs;
+class DumpLeakedURLs {
+ public:
+  DumpLeakedURLs() : mMutex("DumpLeakedURLs::mMutex") {}
+
+  ~DumpLeakedURLs();
+
+  void add(nsStandardURL *aUrl) {
+    OffTheBooksMutexAutoLock lock(mMutex);
+    mURLs.insertBack(aUrl);
+  }
+
+  void remove(nsStandardURL *aUrl) {
+    OffTheBooksMutexAutoLock lock(mMutex);
+    if (aUrl->isInList()) {
+      aUrl->remove();
+    }
+  }
+
+ private:
+  OffTheBooksMutex mMutex;
+  LinkedList<nsStandardURL> mURLs;
+};
+
+DumpLeakedURLs::~DumpLeakedURLs() {
+  MOZ_ASSERT(NS_IsMainThread());
+  OffTheBooksMutexAutoLock lock(mMutex);
+  if (!mURLs.isEmpty()) {
+    printf("Leaked URLs:\n");
+    for (auto url : mURLs) {
+      url->PrintSpec();
+    }
+    mURLs.clear();
+  }
+}
+
+static DumpLeakedURLs gLeakedURLDumper;
+
 #endif
 
 nsStandardURL::nsStandardURL(bool aSupportsFileURL, bool aTrackURL)
     : mDefaultPort(-1),
       mPort(-1),
       mDisplayHost(nullptr),
       mURLType(URLTYPE_STANDARD),
       mSupportsFileURL(aSupportsFileURL),
@@ -210,55 +245,30 @@ nsStandardURL::nsStandardURL(bool aSuppo
   MOZ_ASSERT(gInitialized);
 
   // default parser in case nsIStandardURL::Init is never called
   mParser = net_GetStdURLParser();
 
 #ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
   if (NS_IsMainThread()) {
     if (aTrackURL) {
-      StaticMutexAutoLock lock(gAllURLsMutex);
-      gAllURLs.insertBack(this);
+      gLeakedURLDumper.add(this);
     }
   }
 #endif
 }
 
 nsStandardURL::~nsStandardURL() {
   LOG(("Destroying nsStandardURL @%p\n", this));
 
 #ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
-  {
-    StaticMutexAutoLock lock(gAllURLsMutex);
-    if (isInList()) {
-      remove();
-    }
-  }
+  gLeakedURLDumper.remove(this);
 #endif
 }
 
-#ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
-struct DumpLeakedURLs {
-  DumpLeakedURLs() = default;
-  ~DumpLeakedURLs();
-};
-
-DumpLeakedURLs::~DumpLeakedURLs() {
-  MOZ_ASSERT(NS_IsMainThread());
-  StaticMutexAutoLock lock(gAllURLsMutex);
-  if (!gAllURLs.isEmpty()) {
-    printf("Leaked URLs:\n");
-    for (auto url : gAllURLs) {
-      url->PrintSpec();
-    }
-    gAllURLs.clear();
-  }
-}
-#endif
-
 void nsStandardURL::InitGlobalObjects() {
   MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
 
   if (gInitialized) {
     return;
   }
 
   gInitialized = true;
@@ -275,25 +285,16 @@ void nsStandardURL::InitGlobalObjects() 
   nsCOMPtr<nsIURLParser> parser = net_GetStdURLParser();
   MOZ_DIAGNOSTIC_ASSERT(parser);
   Unused << parser;
 }
 
 void nsStandardURL::ShutdownGlobalObjects() {
   MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
   NS_IF_RELEASE(gIDN);
-
-#ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
-  if (gInitialized) {
-    // This instanciates a dummy class, and will trigger the class
-    // destructor when libxul is unloaded. This is equivalent to atexit(),
-    // but gracefully handles dlclose().
-    static DumpLeakedURLs d;
-  }
-#endif
 }
 
 //----------------------------------------------------------------------------
 // nsStandardURL <private>
 //----------------------------------------------------------------------------
 
 void nsStandardURL::Clear() {
   mSpec.Truncate();