Bug 363024 - Calling createInstance() on mozIStorageService crashes. r=sspitzer
authorsdwilsh@shawnwilsher.com
Mon, 18 Jun 2007 19:22:01 -0700
changeset 2558 3583d775d2c75b5d0768fb35d9f2622970af1c3d
parent 2557 78764a9f450de6919d34fe5b776b8c537b07bc6e
child 2559 ee2eb72d5eeceeb2f53c2a9b0df87ff4cbb29585
push idunknown
push userunknown
push dateunknown
reviewerssspitzer
bugs363024
milestone1.9a6pre
Bug 363024 - Calling createInstance() on mozIStorageService crashes. r=sspitzer
storage/build/mozStorageModule.cpp
storage/src/mozStorageService.cpp
storage/src/mozStorageService.h
--- a/storage/build/mozStorageModule.cpp
+++ b/storage/build/mozStorageModule.cpp
@@ -16,16 +16,17 @@
  *
  * The Initial Developer of the Original Code is
  *  Oracle Corporation
  * Portions created by the Initial Developer are Copyright (C) 2004
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
+ *   Shawn Wilsher <me@shawnwilsher.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -41,17 +42,18 @@
 #include "nsIModule.h"
 
 #include "mozStorageService.h"
 #include "mozStorageConnection.h"
 #include "mozStorageStatementWrapper.h"
 
 #include "mozStorageCID.h"
 
-NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(mozStorageService, Init)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(mozStorageService,
+                                         mozStorageService::GetSingleton)
 NS_GENERIC_FACTORY_CONSTRUCTOR(mozStorageStatementWrapper)
 
 static const nsModuleComponentInfo components[] =
 {
     { "Unified Data Store Service",
       MOZ_STORAGE_SERVICE_CID,
       MOZ_STORAGE_SERVICE_CONTRACTID,
       mozStorageServiceConstructor
--- a/storage/src/mozStorageService.cpp
+++ b/storage/src/mozStorageService.cpp
@@ -47,24 +47,40 @@
 
 #include "sqlite3.h"
 #include "sqlite3file.h"
 
 NS_IMPL_THREADSAFE_ISUPPORTS2(mozStorageService, mozIStorageService, nsIObserver)
 
 static const char kShutdownMessage[] = "xpcom-shutdown-threads";
 
-mozStorageService::mozStorageService()
+mozStorageService *mozStorageService::gStorageService = nsnull;
+
+mozStorageService *
+mozStorageService::GetSingleton()
 {
+    if (gStorageService) {
+        NS_ADDREF(gStorageService);
+        return gStorageService;
+    }
     
+    gStorageService = new mozStorageService();
+    if (gStorageService) {
+        NS_ADDREF(gStorageService);
+        if (NS_FAILED(gStorageService->Init()))
+            NS_RELEASE(gStorageService);
+    }
+    
+    return gStorageService;
 }
 
 mozStorageService::~mozStorageService()
 {
     FreeLocks();
+    gStorageService = nsnull;
 }
 
 nsresult
 mozStorageService::Init()
 {
     // The service must be initialized on the main thread. The
     // InitStorageAsyncIO function creates a thread which is joined with the
     // main thread during shutdown. If the thread is created from a random
--- a/storage/src/mozStorageService.h
+++ b/storage/src/mozStorageService.h
@@ -18,16 +18,17 @@
  * The Initial Developer of the Original Code is
  *  Oracle Corporation
  * Portions created by the Initial Developer are Copyright (C) 2004
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
  *   Brett Wilson <brettw@gmail.com>
+ *   Shawn Wilsher <me@shawnwilsher.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -51,33 +52,35 @@
 class mozStorageConnection;
 
 class mozStorageService : public mozIStorageService,
                           public nsIObserver
 {
     friend class mozStorageConnection;
 
 public:
-    mozStorageService();
-
     // two-phase init, must call before using service
     nsresult Init();
 
+    static mozStorageService *GetSingleton();
+
     // nsISupports
     NS_DECL_ISUPPORTS
 
     // mozIStorageService
     NS_DECL_MOZISTORAGESERVICE
 
     NS_DECL_NSIOBSERVER
 
 private:
-    ~mozStorageService();
+    virtual ~mozStorageService();
 protected:
     nsCOMPtr<nsIFile> mProfileStorageFile;
 
+    static mozStorageService *gStorageService;
+
     nsresult InitStorageAsyncIO();
     nsresult FlushAsyncIO();
     nsresult FinishAsyncIO();
     void FreeLocks();
 };
 
 #endif /* _MOZSTORAGESERVICE_H_ */