Bug 913138 - Shut down imagelib at the end of layout shutdown. r=bsmedberg a=sylvestre
authorBobby Holley <bobbyholley@gmail.com>
Wed, 09 Apr 2014 09:20:15 -0700
changeset 183684 968f7b3ff551
parent 183683 58786efcdbbb
child 183685 5dfea367b8b9
push id3446
push usernalexander@mozilla.com
push date2014-04-09 16:21 +0000
Treeherderresults
reviewersbsmedberg, sylvestre
bugs913138
milestone29.0
Bug 913138 - Shut down imagelib at the end of layout shutdown. r=bsmedberg a=sylvestre
image/build/moz.build
image/build/nsImageModule.cpp
image/build/nsImageModule.h
layout/build/nsLayoutModule.cpp
--- a/image/build/moz.build
+++ b/image/build/moz.build
@@ -1,14 +1,18 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+EXPORTS += [
+    'nsImageModule.h',
+]
+
 SOURCES += [
     'nsImageModule.cpp',
 ]
 
 LIBRARY_NAME = 'imglib2'
 
 FAIL_ON_WARNINGS = True
 
--- a/image/build/nsImageModule.cpp
+++ b/image/build/nsImageModule.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  *
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "nsImageModule.h"
+
 #include "mozilla/ModuleUtils.h"
 #include "nsMimeTypes.h"
 
 #include "ImageFactory.h"
 #include "RasterImage.h"
 #include "SurfaceCache.h"
 
 #include "imgLoader.h"
@@ -74,38 +76,47 @@ static const mozilla::Module::CategoryEn
   { "Gecko-Content-Viewers", IMAGE_BMP_MS, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_ICON_MS, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_PNG, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_X_PNG, "@mozilla.org/content/document-loader-factory;1" },
   { "content-sniffing-services", "@mozilla.org/image/loader;1", "@mozilla.org/image/loader;1" },
   { nullptr }
 };
 
-static nsresult
-imglib_Initialize()
+static bool sInitialized = false;
+nsresult
+mozilla::image::InitModule()
 {
   mozilla::image::DiscardTracker::Initialize();
   mozilla::image::ImageFactory::Initialize();
   mozilla::image::RasterImage::Initialize();
   mozilla::image::SurfaceCache::Initialize();
   imgLoader::GlobalInit();
+  sInitialized = true;
   return NS_OK;
 }
 
-static void
-imglib_Shutdown()
+void
+mozilla::image::ShutdownModule()
 {
+  if (!sInitialized) {
+    return;
+  }
   imgLoader::Shutdown();
   mozilla::image::SurfaceCache::Shutdown();
   mozilla::image::DiscardTracker::Shutdown();
+  sInitialized = false;
 }
 
 static const mozilla::Module kImageModule = {
   mozilla::Module::kVersion,
   kImageCIDs,
   kImageContracts,
   kImageCategories,
   nullptr,
-  imglib_Initialize,
-  imglib_Shutdown
+  mozilla::image::InitModule,
+  // We need to be careful about shutdown ordering to avoid intermittent crashes
+  // when hashtable enumeration decides to destroy modules in an unfortunate
+  // order. So our shutdown is invoked explicitly during layout module shutdown.
+  nullptr
 };
 
 NSMODULE_DEFN(nsImageLib2Module) = &kImageModule;
new file mode 100644
--- /dev/null
+++ b/image/build/nsImageModule.h
@@ -0,0 +1,21 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef nsImageModule_h
+#define nsImageModule_h
+
+#include "nsError.h"
+
+namespace mozilla {
+namespace image {
+
+nsresult InitModule();
+void ShutdownModule();
+
+} /* namespace image */
+} /* namespace mozilla */
+
+
+#endif /* nsImageModule_h */
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -2,16 +2,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "base/basictypes.h"
 
 #include "XPCModule.h"
 #include "mozilla/ModuleUtils.h"
+#include "nsImageModule.h"
 #include "nsLayoutStatics.h"
 #include "nsContentCID.h"
 #include "nsContentDLF.h"
 #include "nsContentPolicyUtils.h"
 #include "nsDataDocumentContentPolicy.h"
 #include "nsNoDataProtocolContentPolicy.h"
 #include "nsDOMCID.h"
 #include "nsHTMLContentSerializer.h"
@@ -1251,18 +1252,19 @@ static const mozilla::Module::CategoryEn
 static void
 LayoutModuleDtor()
 {
   Shutdown();
   nsContentUtils::XPCOMShutdown();
   nsScriptSecurityManager::Shutdown();
   xpcModuleDtor();
 
-  // Layout depends heavily on gfx, so we want to make sure that gfx is shut
-  // down after all the layout cleanup runs.
+  // Layout depends heavily on gfx and imagelib, so we want to make sure that
+  // these modules are shut down after all the layout cleanup runs.
+  mozilla::image::ShutdownModule();
   gfxPlatform::Shutdown();
 }
 
 static const mozilla::Module kLayoutModule = {
   mozilla::Module::kVersion,
   kLayoutCIDs,
   kLayoutContracts,
   kLayoutCategories,