Bug 1040042 - Give nsDirEnumerator a private destructor; r=bsmedberg
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 17 Jul 2014 16:39:41 -0400
changeset 216722 8ec752af1db9b704c9d68bb48a94df246a69f0d1
parent 216721 f5ed82bd6e6d1a03d0bdda0782f86a063bcd70bb
child 216723 547e6057ff302c10d0a4f3bb0de2edb748afdeb4
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1040042
milestone33.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 1040042 - Give nsDirEnumerator a private destructor; r=bsmedberg
xpcom/io/nsLocalFileWin.cpp
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -851,18 +851,23 @@ CloseDir(nsDir*& aDir)
 //-----------------------------------------------------------------------------
 // nsDirEnumerator
 //-----------------------------------------------------------------------------
 
 class nsDirEnumerator MOZ_FINAL
   : public nsISimpleEnumerator
   , public nsIDirectoryEnumerator
 {
+private:
+  ~nsDirEnumerator()
+  {
+    Close();
+  }
+
 public:
-
   NS_DECL_ISUPPORTS
 
   nsDirEnumerator() : mDir(nullptr)
   {
   }
 
   nsresult Init(nsIFile* aParent)
   {
@@ -964,23 +969,16 @@ public:
       NS_ASSERTION(NS_SUCCEEDED(rv), "close failed");
       if (NS_FAILED(rv)) {
         return NS_ERROR_FAILURE;
       }
     }
     return NS_OK;
   }
 
-  // dtor can be non-virtual since there are no subclasses, but must be
-  // public to use the class on the stack.
-  ~nsDirEnumerator()
-  {
-    Close();
-  }
-
 protected:
   nsDir*             mDir;
   nsCOMPtr<nsIFile>  mParent;
   nsCOMPtr<nsIFile>  mNext;
 };
 
 NS_IMPL_ISUPPORTS(nsDirEnumerator, nsISimpleEnumerator, nsIDirectoryEnumerator)
 
@@ -2099,29 +2097,29 @@ nsLocalFile::CopyMove(nsIFile* aParentDi
       bool more;
       targetIterator->HasMoreElements(&more);
       // return error if target directory is not empty
       if (more) {
         return NS_ERROR_FILE_DIR_NOT_EMPTY;
       }
     }
 
-    nsDirEnumerator dirEnum;
-
-    rv = dirEnum.Init(this);
+    nsRefPtr<nsDirEnumerator> dirEnum = new nsDirEnumerator();
+
+    rv = dirEnum->Init(this);
     if (NS_FAILED(rv)) {
       NS_WARNING("dirEnum initialization failed");
       return rv;
     }
 
     bool more = false;
-    while (NS_SUCCEEDED(dirEnum.HasMoreElements(&more)) && more) {
+    while (NS_SUCCEEDED(dirEnum->HasMoreElements(&more)) && more) {
       nsCOMPtr<nsISupports> item;
       nsCOMPtr<nsIFile> file;
-      dirEnum.GetNext(getter_AddRefs(item));
+      dirEnum->GetNext(getter_AddRefs(item));
       file = do_QueryInterface(item);
       if (file) {
         bool isDir, isLink;
 
         file->IsDirectory(&isDir);
         file->IsSymlink(&isLink);
 
         if (move) {
@@ -2331,27 +2329,27 @@ nsLocalFile::Remove(bool aRecursive)
     rv = IsDirectory(&isDir);
     if (NS_FAILED(rv)) {
       return rv;
     }
   }
 
   if (isDir) {
     if (aRecursive) {
-      nsDirEnumerator dirEnum;
-
-      rv = dirEnum.Init(this);
+      nsRefPtr<nsDirEnumerator> dirEnum = new nsDirEnumerator();
+
+      rv = dirEnum->Init(this);
       if (NS_FAILED(rv)) {
         return rv;
       }
 
       bool more = false;
-      while (NS_SUCCEEDED(dirEnum.HasMoreElements(&more)) && more) {
+      while (NS_SUCCEEDED(dirEnum->HasMoreElements(&more)) && more) {
         nsCOMPtr<nsISupports> item;
-        dirEnum.GetNext(getter_AddRefs(item));
+        dirEnum->GetNext(getter_AddRefs(item));
         nsCOMPtr<nsIFile> file = do_QueryInterface(item);
         if (file) {
           file->Remove(aRecursive);
         }
       }
     }
     if (RemoveDirectoryW(mWorkingPath.get()) == 0) {
       return ConvertWinError(GetLastError());
@@ -3200,27 +3198,23 @@ nsLocalFile::GetDirectoryEntries(nsISimp
     if (NS_FAILED(rv)) {
       NS_RELEASE(drives);
       return rv;
     }
     *aEntries = drives;
     return NS_OK;
   }
 
-  nsDirEnumerator* dirEnum = new nsDirEnumerator();
-  if (dirEnum == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-  NS_ADDREF(dirEnum);
+  nsRefPtr<nsDirEnumerator> dirEnum = new nsDirEnumerator();
   rv = dirEnum->Init(this);
   if (NS_FAILED(rv)) {
-    NS_RELEASE(dirEnum);
     return rv;
   }
 
-  *aEntries = dirEnum;
+  dirEnum.forget(aEntries);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLocalFile::GetPersistentDescriptor(nsACString& aPersistentDescriptor)
 {
   CopyUTF16toUTF8(mWorkingPath, aPersistentDescriptor);