manual-stack-comptr-cleanup
author Benjamin Smedberg <benjamin@smedbergs.us>
Sat, 26 Jul 2008 22:49:39 -0400
changeset 167 a4da40849f5436e629c5732f4368c6c48189637f
parent 95 b35cbfa6ad4f8bac97b5a0ae2a3bfc18ed88de35
permissions -rw-r--r--
State as of now

* * *

diff --git a/content/base/public/nsContentPolicyUtils.h b/content/base/public/nsContentPolicyUtils.h
--- a/content/base/public/nsContentPolicyUtils.h
+++ b/content/base/public/nsContentPolicyUtils.h
@@ -152,7 +152,7 @@ NS_CP_ContentTypeName(PRUint32 contentTy
 /* Passes on parameters from its "caller"'s context. */
 #define CHECK_CONTENT_POLICY(action)                                          \
   PR_BEGIN_MACRO                                                              \
-    nsCOMPtr<nsIContentPolicy> policy =                                       \
+    nsIContentPolicy* policy =                                                \
          do_GetService(NS_CONTENTPOLICY_CONTRACTID);                          \
     if (!policy)                                                              \
         return NS_ERROR_FAILURE;                                              \
@@ -175,10 +175,10 @@ NS_CP_ContentTypeName(PRUint32 contentTy
  * Note: requestOrigin is scoped outside the PR_BEGIN_MACRO/PR_END_MACRO on
  * purpose */
 #define CHECK_PRINCIPAL                                                       \
-  nsCOMPtr<nsIURI> requestOrigin;                                             \
+  nsIURI* requestOrigin = nsnull;                                             \
   PR_BEGIN_MACRO                                                              \
   if (originPrincipal) {                                                      \
-      nsCOMPtr<nsIScriptSecurityManager> secMan = aSecMan;                    \
+      nsIScriptSecurityManager* secMan = aSecMan;                             \
       if (!secMan) {                                                          \
           secMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);        \
       }                                                                       \
@@ -192,7 +192,7 @@ NS_CP_ContentTypeName(PRUint32 contentTy
               return NS_OK;                                                   \
           }                                                                   \
       }                                                                       \
-      nsresult rv = originPrincipal->GetURI(getter_AddRefs(requestOrigin));   \
+      nsresult rv = originPrincipal->GetURI(&requestOrigin);                  \
       NS_ENSURE_SUCCESS(rv, rv);                                              \
   }                                                                           \
   PR_END_MACRO
diff --git a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp
--- a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp
+++ b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp
@@ -1852,21 +1852,19 @@ void nsWebBrowserPersist::CleanupLocalFi
                 // recursed through to ensure they are actually empty.
 
                 PRBool isEmptyDirectory = PR_TRUE;
-                nsSupportsArray dirStack;
+                nsCOMArray<nsISimpleEnumerator> dirStack;
                 PRUint32 stackSize = 0;
 
                 // Push the top level enum onto the stack
                 nsCOMPtr<nsISimpleEnumerator> pos;
                 if (NS_SUCCEEDED(file->GetDirectoryEntries(getter_AddRefs(pos))))
-                    dirStack.AppendElement(pos);
-
-                while (isEmptyDirectory &&
-                    NS_SUCCEEDED(dirStack.Count(&stackSize)) && stackSize > 0)
+                    dirStack.AppendObject(pos);
+
+                while (isEmptyDirectory && dirStack.Count() > 0)
                 {
                     // Pop the last element
-                    nsCOMPtr<nsISimpleEnumerator> curPos;
-                    dirStack.GetElementAt(stackSize - 1, getter_AddRefs(curPos));
-                    dirStack.RemoveElementAt(stackSize - 1);
+                    nsCOMPtr<nsISimpleEnumerator> curPos = dirStack[dirStack.Count() - 1];
+                    dirStack.RemoveObjectAt(dirStack.Count() - 1);
                     
                     // Test if the enumerator has any more files in it
                     PRBool hasMoreElements = PR_FALSE;
@@ -1900,9 +1898,9 @@ void nsWebBrowserPersist::CleanupLocalFi
                     // Push parent enumerator followed by child enumerator
                     nsCOMPtr<nsISimpleEnumerator> childPos;
                     childAsFile->GetDirectoryEntries(getter_AddRefs(childPos));
-                    dirStack.AppendElement(curPos);
+                    dirStack.AppendObject(curPos);
                     if (childPos)
-                        dirStack.AppendElement(childPos);
+                        dirStack.AppendObject(childPos);
 
                 }
                 dirStack.Clear();
diff --git a/layout/generic/nsSelection.cpp b/layout/generic/nsSelection.cpp
--- a/layout/generic/nsSelection.cpp
+++ b/layout/generic/nsSelection.cpp
@@ -3922,7 +3922,7 @@ nsFrameSelection::DeleteFromDocument()
   }
 
   // Get an iterator
-  nsSelectionIterator iter(mDomSelections[index]);
+  nsSelectionIterator &iter = *new nsSelectionIterator(mDomSelections[index]);
   res = iter.First();
   if (NS_FAILED(res))
     return res;
diff --git a/layout/mathml/base/src/nsMathMLChar.cpp b/layout/mathml/base/src/nsMathMLChar.cpp
--- a/layout/mathml/base/src/nsMathMLChar.cpp
+++ b/layout/mathml/base/src/nsMathMLChar.cpp
@@ -781,7 +781,6 @@ InitGlobals(nsPresContext* aPresContext)
     rv = gGlyphTableList->Initialize();
   }
   if (NS_FAILED(rv)) {
-    delete gGlyphTableList;
     gGlyphTableList = nsnull;
     return rv;
   }
diff --git a/layout/svg/base/src/nsSVGOuterSVGFrame.cpp b/layout/svg/base/src/nsSVGOuterSVGFrame.cpp
--- a/layout/svg/base/src/nsSVGOuterSVGFrame.cpp
+++ b/layout/svg/base/src/nsSVGOuterSVGFrame.cpp
@@ -78,7 +78,7 @@ NS_INTERFACE_MAP_BEGIN(nsSVGMutationObse
   NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
 NS_INTERFACE_MAP_END
 
-static nsSVGMutationObserver sSVGMutationObserver;
+static nsSVGMutationObserver *gSVGMutationObserver;
 
 //----------------------------------------------------------------------
 // nsIMutationObserver methods
@@ -176,10 +176,16 @@ nsSVGOuterSVGFrame::Init(nsIContent* aCo
       SVGElement->GetCurrentTranslate(getter_AddRefs(mCurrentTranslate));
       SVGElement->GetCurrentScaleNumber(getter_AddRefs(mCurrentScale));
     }
+
+    if (!gSVGMutationObserver) {
+      gSVGMutationObserver = new nsSVGMutationObserver();
+      NS_RootUntilShutdown(gSVGMutationObserver);
+    }
+
     // AddMutationObserver checks that the observer is not already added.
     // sSVGMutationObserver has the same lifetime as the document so does
     // not need to be removed
-    doc->AddMutationObserver(&sSVGMutationObserver);
+    doc->AddMutationObserver(gSVGMutationObserver);
   }
 
   SuspendRedraw();  // UnsuspendRedraw is in DidReflow
diff --git a/parser/htmlparser/src/CNavDTD.cpp b/parser/htmlparser/src/CNavDTD.cpp
--- a/parser/htmlparser/src/CNavDTD.cpp
+++ b/parser/htmlparser/src/CNavDTD.cpp
@@ -160,14 +160,13 @@ GetLoggingSink()
   
 
   if (gLogPath && !theSink) {
-    static nsLoggingSink gLoggingSink;
-
     PRIntn theFlags = PR_CREATE_FILE | PR_RDWR;
 
     // Open the record file.
     PRFileDesc *theLogFile = PR_Open(gLogPath, theFlags, 0);
-    gLoggingSink.SetOutputStream(theLogFile, PR_TRUE);
-    theSink = &gLoggingSink;
+    theSink = new nsLoggingSink();
+    theSink->SetOutputStream(theLogFile, PR_TRUE);
+    NS_RootUntilShutdown(theSink);
   }
 
   return theSink;
diff --git a/security/manager/ssl/src/nsIdentityChecking.cpp b/security/manager/ssl/src/nsIdentityChecking.cpp
--- a/security/manager/ssl/src/nsIdentityChecking.cpp
+++ b/security/manager/ssl/src/nsIdentityChecking.cpp
@@ -188,7 +188,7 @@ static PRBool isEVMatch(SECOidTag policy
 
   NS_ConvertASCIItoUTF16 info_sha1(info.ev_root_sha1_fingerprint);
 
-  nsNSSCertificate c(rootCert);
+  nsNSSCertificate &c = *new nsNSSCertificate(rootCert);
 
   nsAutoString fingerprint;
   if (NS_FAILED(c.GetSha1Fingerprint(fingerprint)))
diff --git a/security/manager/ssl/src/nsNSSCertificateDB.cpp b/security/manager/ssl/src/nsNSSCertificateDB.cpp
--- a/security/manager/ssl/src/nsNSSCertificateDB.cpp
+++ b/security/manager/ssl/src/nsNSSCertificateDB.cpp
@@ -1321,7 +1321,7 @@ nsNSSCertificateDB::getCertNames(CERTCer
        !CERT_LIST_END(node, certList);
        node = CERT_LIST_NEXT(node)) {
     if (getCertType(node->cert) == type) {
-      nsNSSCertificate pipCert(node->cert);
+      nsNSSCertificate &pipCert = *new nsNSSCertificate(node->cert);
       char *dbkey = NULL;
       char *namestr = NULL;
       nsAutoString certstr;
diff --git a/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp b/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp
--- a/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp
+++ b/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp
@@ -64,7 +64,6 @@ void TestUnescapeHelper(const char* in, 
 void TestUnescapeHelper(const char* in, const char* expected)
 {
   nsCString out, strIn(in), strExp(expected);
-  nsUrlClassifierUtils utils;
   
   NS_UnescapeURL(strIn.get(), strIn.Length(), esc_AlwaysCopy, out);
   CheckEquals(strExp, out);
@@ -118,7 +117,7 @@ void TestEncodeHelper(const char* in, co
 void TestEncodeHelper(const char* in, const char* expected)
 {
   nsCString out, strIn(in), strExp(expected);
-  nsUrlClassifierUtils utils;
+  nsUrlClassifierUtils &utils = *new nsUrlClassifierUtils; 
 
   utils.SpecialEncode(strIn, PR_TRUE, out);
   CheckEquals(strExp, out);
@@ -136,7 +135,7 @@ void TestEnc()
       noenc.Append(static_cast<char>(i));
     }
   }
-  nsUrlClassifierUtils utils;
+  nsUrlClassifierUtils &utils = *new nsUrlClassifierUtils;
   nsCString out;
   utils.SpecialEncode(noenc, PR_FALSE, out);
   CheckEquals(noenc, out);
@@ -162,7 +161,7 @@ void TestCanonicalizeHelper(const char* 
 void TestCanonicalizeHelper(const char* in, const char* expected)
 {
   nsCString out, strIn(in), strExp(expected);
-  nsUrlClassifierUtils utils;
+  nsUrlClassifierUtils &utils = *new nsUrlClassifierUtils;
 
   utils.CanonicalizePath(strIn, out);
   CheckEquals(strExp, out);
@@ -190,7 +189,7 @@ void TestParseIPAddressHelper(const char
 void TestParseIPAddressHelper(const char *in, const char *expected)
 {
   nsCString out, strIn(in), strExp(expected);
-  nsUrlClassifierUtils utils;
+  nsUrlClassifierUtils &utils = *new nsUrlClassifierUtils;
   utils.Init();
 
   utils.ParseIPAddress(strIn, out);
@@ -223,7 +222,7 @@ void TestCanonicalNumHelper(const char *
                             bool allowOctal, const char *expected)
 {
   nsCString out, strIn(in), strExp(expected);
-  nsUrlClassifierUtils utils;
+  nsUrlClassifierUtils &utils = *new nsUrlClassifierUtils;
   utils.Init();
 
   utils.CanonicalNum(strIn, bytes, allowOctal, out);
@@ -251,7 +250,7 @@ void TestHostnameHelper(const char *in, 
 void TestHostnameHelper(const char *in, const char *expected)
 {
   nsCString out, strIn(in), strExp(expected);
-  nsUrlClassifierUtils utils;
+  nsUrlClassifierUtils &utils = *new nsUrlClassifierUtils;
   utils.Init();
 
   utils.CanonicalizeHostname(strIn, out);
@@ -288,7 +287,7 @@ void TestLongHostname()
   memset(str, 'x', kTestSize);
   str[kTestSize] = '\0';
 
-  nsUrlClassifierUtils utils;
+  nsUrlClassifierUtils &utils = *new nsUrlClassifierUtils;
   utils.Init();
 
   nsCAutoString out;
diff --git a/xpcom/tests/TestExpirationTracker.cpp b/xpcom/tests/TestExpirationTracker.cpp
--- a/xpcom/tests/TestExpirationTracker.cpp
+++ b/xpcom/tests/TestExpirationTracker.cpp
@@ -173,7 +173,7 @@ template <PRUint32 K> static PRBool test
   error = PR_FALSE;
  
   for (PRUint32 j = 0; j < iterations; ++j) {
-    Tracker<K> tracker;
+    Tracker<K> &tracker = *new Tracker<K>;
 
     PRUint32 i = 0;
     for (i = 0; i < ops; ++i) {