fix Mutex reported memory leak in unit tests, r=neil, bug 650508
authorDavid Bienvenu <bienvenu@nventure.com>
Thu, 30 Jun 2011 08:10:10 -0700
changeset 8247 f03b17b338ef31cbddb0b305c269c8b4bd4b5418
parent 8246 0a3c60c01adff4ddfbdbb4de7fc72641f99d1886
child 8248 0bde656fe3aeb9f55308d611cd8ad9fb1412bb08
push id84
push userbugzilla@standard8.plus.com
push dateTue, 16 Aug 2011 21:25:04 +0000
treeherdercomm-beta@6970c86be3cd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersneil, bug
bugs650508
fix Mutex reported memory leak in unit tests, r=neil, bug 650508
mailnews/addrbook/src/nsAbWinHelper.cpp
mailnews/addrbook/src/nsAbWinHelper.h
mailnews/addrbook/src/nsMapiAddressBook.cpp
mailnews/addrbook/src/nsWabAddressBook.cpp
--- a/mailnews/addrbook/src/nsAbWinHelper.cpp
+++ b/mailnews/addrbook/src/nsAbWinHelper.cpp
@@ -261,35 +261,40 @@ void nsMapiEntryArray::CleanUp(void)
         delete [] mEntries ;
         mEntries = NULL ;
         mNbEntries = 0 ;
     }
 }
 
 using namespace mozilla;
 
-PRUint32 nsAbWinHelper::mEntryCounter = 0 ;
-Mutex nsAbWinHelper::mMutex("nsAbWinHelper.mMutex");
-
+PRUint32 nsAbWinHelper::mEntryCounter = 0;
+nsAutoPtr<mozilla::Mutex> nsAbWinHelper::mMutex;
+PRUint32 nsAbWinHelper::mUseCount = 0;
 // There seems to be a deadlock/auto-destruction issue
 // in MAPI when multiple threads perform init/release 
 // operations at the same time. So I've put a mutex
 // around both the initialize process and the destruction
 // one. I just hope the rest of the calls don't need the 
 // same protection (MAPI is supposed to be thread-safe).
 
 nsAbWinHelper::nsAbWinHelper(void)
 : mAddressBook(NULL), mLastError(S_OK)
 {
-    MOZ_COUNT_CTOR(nsAbWinHelper) ;
+  if (!mUseCount++)
+    mMutex = new mozilla::Mutex("nsAbWinHelper.mMutex");
+
+  MOZ_COUNT_CTOR(nsAbWinHelper);
 }
 
 nsAbWinHelper::~nsAbWinHelper(void)
 {
-    MOZ_COUNT_DTOR(nsAbWinHelper) ;
+  if (!--mUseCount)
+    mMutex = nsnull;
+  MOZ_COUNT_DTOR(nsAbWinHelper) ;
 }
 
 BOOL nsAbWinHelper::GetFolders(nsMapiEntryArray& aFolders)
 {
     aFolders.CleanUp() ;
     nsMapiInterfaceWrapper<LPABCONT> rootFolder ;
     nsMapiInterfaceWrapper<LPMAPITABLE> folders ;
     ULONG objType = 0 ;
--- a/mailnews/addrbook/src/nsAbWinHelper.h
+++ b/mailnews/addrbook/src/nsAbWinHelper.h
@@ -38,17 +38,18 @@
 #ifndef nsAbWinHelper_h___
 #define nsAbWinHelper_h___
 
 #include <windows.h>
 #include <mapix.h>
 
 #include "nsStringGlue.h"
 #include "mozilla/Mutex.h"
- 
+#include "nsAutoPtr.h"
+
 struct nsMapiEntry
 {
     ULONG     mByteCount ;
     LPENTRYID mEntryId ;
 
     nsMapiEntry(void) ;
     ~nsMapiEntry(void) ;
     nsMapiEntry(ULONG aByteCount, LPENTRYID aEntryId) ;
@@ -125,17 +126,18 @@ public:
     BOOL GetDefaultContainer(nsMapiEntry& aContainer) ;
     // Is the helper correctly initialised?
     BOOL IsOK(void) const { return mAddressBook != NULL ; }
 
 protected:
     HRESULT mLastError ;
     LPADRBOOK mAddressBook ;
     static PRUint32 mEntryCounter ;
-    static mozilla::Mutex mMutex ;
+    static PRUint32 mUseCount ;
+    static nsAutoPtr<mozilla::Mutex> mMutex ;
 
     // Retrieve the contents of a container, with an optional restriction
     BOOL GetContents(const nsMapiEntry& aParent, LPSRestriction aRestriction, 
                      nsMapiEntry **aList, ULONG &aNbElements, ULONG aMapiType) ;
     // Retrieve the values of a set of properties on a MAPI object
     BOOL GetMAPIProperties(const nsMapiEntry& aObject, const ULONG *aPropertyTags, 
                            ULONG aNbProperties,
                            LPSPropValue& aValues, ULONG& aValueCount) ;
--- a/mailnews/addrbook/src/nsMapiAddressBook.cpp
+++ b/mailnews/addrbook/src/nsMapiAddressBook.cpp
@@ -140,26 +140,26 @@ nsMapiAddressBook::nsMapiAddressBook(voi
     BOOL result = Initialize() ;
 
     NS_ASSERTION(result == TRUE, "Couldn't initialize Mapi Helper") ;
     MOZ_COUNT_CTOR(nsMapiAddressBook) ;
 }
 
 nsMapiAddressBook::~nsMapiAddressBook(void)
 {
-    MutexAutoLock guard(mMutex) ;
+    MutexAutoLock guard(*mMutex) ;
 
     FreeMapiLibrary() ;
     MOZ_COUNT_DTOR(nsMapiAddressBook) ;
 }
 
 BOOL nsMapiAddressBook::Initialize(void)
 {
     if (mAddressBook) { return TRUE ; }
-    MutexAutoLock guard(mMutex) ;
+    MutexAutoLock guard(*mMutex) ;
 
     if (!LoadMapiLibrary()) {
         PRINTF(("Cannot load library.\n")) ;
         return FALSE ;
     }
     mAddressBook = mRootBook ; 
     return TRUE ;
 }
--- a/mailnews/addrbook/src/nsWabAddressBook.cpp
+++ b/mailnews/addrbook/src/nsWabAddressBook.cpp
@@ -98,25 +98,25 @@ nsWabAddressBook::nsWabAddressBook(void)
     BOOL result = Initialize() ;
 
     NS_ASSERTION(result == TRUE, "Couldn't initialize Wab Helper") ;
     MOZ_COUNT_CTOR(nsWabAddressBook) ;
 }
 
 nsWabAddressBook::~nsWabAddressBook(void)
 {
-    MutexAutoLock guard(mMutex) ;
+    MutexAutoLock guard(*mMutex) ;
     FreeWabLibrary() ;
     MOZ_COUNT_DTOR(nsWabAddressBook) ;
 }
 
 BOOL nsWabAddressBook::Initialize(void)
 {
     if (mAddressBook) { return TRUE ; }
-    MutexAutoLock guard(mMutex) ;
+    MutexAutoLock guard(*mMutex) ;
 
     if (!LoadWabLibrary()) {
         PRINTF(("Cannot load library.\n")) ;
         return FALSE ;
     }
     mAddressBook = mRootBook ;
     return TRUE ;
 }