Bug 1343866 - Scope more nsCOMPtrs in XRE_XPCShellMain(); r=bsmedberg
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 01 Mar 2017 22:54:24 -0500
changeset 346122 fde4cd0aed6deccd27113ad6519ad31d5f27564a
parent 346121 8086f1e5317b27a3e2a344c5057f51ea1ef337dc
child 346123 4031cd5354cfac3db99b6e7c440639243c9f86cd
push id87734
push usereakhgari@mozilla.com
push dateMon, 06 Mar 2017 20:06:24 +0000
treeherdermozilla-inbound@fde4cd0aed6d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1343866
milestone54.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 1343866 - Scope more nsCOMPtrs in XRE_XPCShellMain(); r=bsmedberg
js/xpconnect/src/XPCShellImpl.cpp
--- a/js/xpconnect/src/XPCShellImpl.cpp
+++ b/js/xpconnect/src/XPCShellImpl.cpp
@@ -1318,125 +1318,125 @@ XRE_XPCShellMain(int argc, char** argv, 
         }
         ChaosMode::SetChaosFeature(feature);
     }
 
     if (ChaosMode::isActive(ChaosFeature::Any)) {
         printf_stderr("*** You are running in chaos test mode. See ChaosMode.h. ***\n");
     }
 
-    nsCOMPtr<nsIFile> appFile;
-    rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(appFile));
-    if (NS_FAILED(rv)) {
-        printf("Couldn't find application file.\n");
-        return 1;
-    }
-    nsCOMPtr<nsIFile> appDir;
-    rv = appFile->GetParent(getter_AddRefs(appDir));
-    if (NS_FAILED(rv)) {
-        printf("Couldn't get application directory.\n");
-        return 1;
-    }
-
-    XPCShellDirProvider dirprovider;
-
-    dirprovider.SetAppFile(appFile);
-
-    nsCOMPtr<nsIFile> greDir;
-    if (argc > 1 && !strcmp(argv[1], "-g")) {
-        if (argc < 3)
-            return usage();
-
-        rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(greDir));
+    { // Start scoping nsCOMPtrs
+        nsCOMPtr<nsIFile> appFile;
+        rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(appFile));
         if (NS_FAILED(rv)) {
-            printf("Couldn't use given GRE dir.\n");
+            printf("Couldn't find application file.\n");
+            return 1;
+        }
+        nsCOMPtr<nsIFile> appDir;
+        rv = appFile->GetParent(getter_AddRefs(appDir));
+        if (NS_FAILED(rv)) {
+            printf("Couldn't get application directory.\n");
             return 1;
         }
 
-        dirprovider.SetGREDirs(greDir);
+        XPCShellDirProvider dirprovider;
+
+        dirprovider.SetAppFile(appFile);
+
+        nsCOMPtr<nsIFile> greDir;
+        if (argc > 1 && !strcmp(argv[1], "-g")) {
+            if (argc < 3)
+                return usage();
 
-        argc -= 2;
-        argv += 2;
-    } else {
+            rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(greDir));
+            if (NS_FAILED(rv)) {
+                printf("Couldn't use given GRE dir.\n");
+                return 1;
+            }
+
+            dirprovider.SetGREDirs(greDir);
+
+            argc -= 2;
+            argv += 2;
+        } else {
 #ifdef XP_MACOSX
-        // On OSX, the GreD needs to point to Contents/Resources in the .app
-        // bundle. Libraries will be loaded at a relative path to GreD, i.e.
-        // ../MacOS.
-        nsCOMPtr<nsIFile> tmpDir;
-        XRE_GetFileFromPath(argv[0], getter_AddRefs(greDir));
-        greDir->GetParent(getter_AddRefs(tmpDir));
-        tmpDir->Clone(getter_AddRefs(greDir));
-        tmpDir->SetNativeLeafName(NS_LITERAL_CSTRING("Resources"));
-        bool dirExists = false;
-        tmpDir->Exists(&dirExists);
-        if (dirExists) {
-            greDir = tmpDir.forget();
+            // On OSX, the GreD needs to point to Contents/Resources in the .app
+            // bundle. Libraries will be loaded at a relative path to GreD, i.e.
+            // ../MacOS.
+            nsCOMPtr<nsIFile> tmpDir;
+            XRE_GetFileFromPath(argv[0], getter_AddRefs(greDir));
+            greDir->GetParent(getter_AddRefs(tmpDir));
+            tmpDir->Clone(getter_AddRefs(greDir));
+            tmpDir->SetNativeLeafName(NS_LITERAL_CSTRING("Resources"));
+            bool dirExists = false;
+            tmpDir->Exists(&dirExists);
+            if (dirExists) {
+                greDir = tmpDir.forget();
+            }
+            dirprovider.SetGREDirs(greDir);
+#else
+            nsAutoString workingDir;
+            if (!GetCurrentWorkingDirectory(workingDir)) {
+                printf("GetCurrentWorkingDirectory failed.\n");
+                return 1;
+            }
+            rv = NS_NewLocalFile(workingDir, true, getter_AddRefs(greDir));
+            if (NS_FAILED(rv)) {
+                printf("NS_NewLocalFile failed.\n");
+                return 1;
+            }
+#endif
         }
-        dirprovider.SetGREDirs(greDir);
-#else
-        nsAutoString workingDir;
-        if (!GetCurrentWorkingDirectory(workingDir)) {
-            printf("GetCurrentWorkingDirectory failed.\n");
-            return 1;
+
+        if (argc > 1 && !strcmp(argv[1], "-a")) {
+            if (argc < 3)
+                return usage();
+
+            nsCOMPtr<nsIFile> dir;
+            rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(dir));
+            if (NS_SUCCEEDED(rv)) {
+                appDir = do_QueryInterface(dir, &rv);
+                dirprovider.SetAppDir(appDir);
+            }
+            if (NS_FAILED(rv)) {
+                printf("Couldn't use given appdir.\n");
+                return 1;
+            }
+            argc -= 2;
+            argv += 2;
         }
-        rv = NS_NewLocalFile(workingDir, true, getter_AddRefs(greDir));
-        if (NS_FAILED(rv)) {
-            printf("NS_NewLocalFile failed.\n");
-            return 1;
+
+        while (argc > 1 && !strcmp(argv[1], "-r")) {
+            if (argc < 3)
+                return usage();
+
+            nsCOMPtr<nsIFile> lf;
+            rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(lf));
+            if (NS_FAILED(rv)) {
+                printf("Couldn't get manifest file.\n");
+                return 1;
+            }
+            XRE_AddManifestLocation(NS_APP_LOCATION, lf);
+
+            argc -= 2;
+            argv += 2;
+        }
+
+#ifdef MOZ_CRASHREPORTER
+        const char* val = getenv("MOZ_CRASHREPORTER");
+        if (val && *val) {
+            rv = CrashReporter::SetExceptionHandler(greDir, true);
+            if (NS_FAILED(rv)) {
+                printf("CrashReporter::SetExceptionHandler failed!\n");
+                return 1;
+            }
+            MOZ_ASSERT(CrashReporter::GetEnabled());
         }
 #endif
-    }
 
-    if (argc > 1 && !strcmp(argv[1], "-a")) {
-        if (argc < 3)
-            return usage();
-
-        nsCOMPtr<nsIFile> dir;
-        rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(dir));
-        if (NS_SUCCEEDED(rv)) {
-            appDir = do_QueryInterface(dir, &rv);
-            dirprovider.SetAppDir(appDir);
-        }
-        if (NS_FAILED(rv)) {
-            printf("Couldn't use given appdir.\n");
-            return 1;
-        }
-        argc -= 2;
-        argv += 2;
-    }
-
-    while (argc > 1 && !strcmp(argv[1], "-r")) {
-        if (argc < 3)
-            return usage();
-
-        nsCOMPtr<nsIFile> lf;
-        rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(lf));
-        if (NS_FAILED(rv)) {
-            printf("Couldn't get manifest file.\n");
-            return 1;
-        }
-        XRE_AddManifestLocation(NS_APP_LOCATION, lf);
-
-        argc -= 2;
-        argv += 2;
-    }
-
-#ifdef MOZ_CRASHREPORTER
-    const char* val = getenv("MOZ_CRASHREPORTER");
-    if (val && *val) {
-        rv = CrashReporter::SetExceptionHandler(greDir, true);
-        if (NS_FAILED(rv)) {
-            printf("CrashReporter::SetExceptionHandler failed!\n");
-            return 1;
-        }
-        MOZ_ASSERT(CrashReporter::GetEnabled());
-    }
-#endif
-
-    {
         if (argc > 1 && !strcmp(argv[1], "--greomni")) {
             nsCOMPtr<nsIFile> greOmni;
             nsCOMPtr<nsIFile> appOmni;
             XRE_GetFileFromPath(argv[2], getter_AddRefs(greOmni));
             if (argc > 3 && !strcmp(argv[3], "--appomni")) {
                 XRE_GetFileFromPath(argv[4], getter_AddRefs(appOmni));
                 argc-=2;
                 argv+=2;
@@ -1621,16 +1621,21 @@ XRE_XPCShellMain(int argc, char** argv, 
             }
 
             JS_DropPrincipals(cx, gJSPrincipals);
             JS_SetAllNonReservedSlotsToUndefined(cx, glob);
             JS_SetAllNonReservedSlotsToUndefined(cx, JS_GlobalLexicalEnvironment(glob));
             JS_GC(cx);
         }
         JS_GC(cx);
+
+        dirprovider.ClearGREDirs();
+        dirprovider.ClearAppDir();
+        dirprovider.ClearPluginDir();
+        dirprovider.ClearAppFile();
     } // this scopes the nsCOMPtrs
 
     if (!XRE_ShutdownTestShell())
         NS_ERROR("problem shutting down testshell");
 
     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
     rv = NS_ShutdownXPCOM( nullptr );
     MOZ_ASSERT(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
@@ -1638,22 +1643,16 @@ XRE_XPCShellMain(int argc, char** argv, 
 #ifdef TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN
     // test of late call and release (see above)
     JSContext* bogusCX;
     bogus->Peek(&bogusCX);
     bogus = nullptr;
 #endif
 
     telStats = nullptr;
-    appDir = nullptr;
-    appFile = nullptr;
-    dirprovider.ClearGREDirs();
-    dirprovider.ClearAppDir();
-    dirprovider.ClearPluginDir();
-    dirprovider.ClearAppFile();
 
 #ifdef MOZ_CRASHREPORTER
     // Shut down the crashreporter service to prevent leaking some strings it holds.
     if (CrashReporter::GetEnabled())
         CrashReporter::UnsetExceptionHandler();
 #endif
 
     // This must precede NS_LogTerm(), otherwise xpcshell return non-zero