Bug 901976 - don't null-check |new X| in xpcom/; r=bsmedberg
authorNathan Froyd <froydnj@mozilla.com>
Mon, 05 Aug 2013 11:16:53 -0400
changeset 141867 3d5845e606affae6375094cb8ce603939e25e45f
parent 141866 681cd35e0e5e147d3112f30be356d47f7b731264
child 141868 45177d61c1c5aae77f2fd9395d79dc39a287e4a2
push id32263
push usernfroyd@mozilla.com
push dateFri, 09 Aug 2013 14:34:19 +0000
treeherdermozilla-inbound@a3ea5bc816f5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs901976
milestone26.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 901976 - don't null-check |new X| in xpcom/; r=bsmedberg
xpcom/base/nsErrorService.cpp
xpcom/ds/nsAtomTable.cpp
xpcom/ds/nsByteBuffer.cpp
xpcom/ds/nsCheapSets.h
xpcom/ds/nsHashPropertyBag.cpp
xpcom/ds/nsObserverList.cpp
xpcom/ds/nsUnicharBuffer.cpp
xpcom/ds/nsWindowsRegKey.cpp
xpcom/io/nsLocalFileUnix.cpp
--- a/xpcom/base/nsErrorService.cpp
+++ b/xpcom/base/nsErrorService.cpp
@@ -1,15 +1,16 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* 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 "nsErrorService.h"
 #include "nsCRT.h"
+#include "nsAutoPtr.h"
 
 static void*
 CloneCString(nsHashKey *aKey, void *aData, void* closure)
 {
   return NS_strdup((const char*)aData);
 }
 
 static bool
@@ -60,23 +61,18 @@ nsInt2StrHashtable::Remove(uint32_t key)
 ////////////////////////////////////////////////////////////////////////////////
 
 NS_IMPL_ISUPPORTS1(nsErrorService, nsIErrorService)
 
 nsresult
 nsErrorService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
 {
     NS_ENSURE_NO_AGGREGATION(outer);
-    nsErrorService* serv = new nsErrorService();
-    if (serv == nullptr)
-        return NS_ERROR_OUT_OF_MEMORY;
-    NS_ADDREF(serv);
-    nsresult rv = serv->QueryInterface(aIID, aInstancePtr);
-    NS_RELEASE(serv);
-    return rv;
+    nsRefPtr<nsErrorService> serv = new nsErrorService();
+    return serv->QueryInterface(aIID, aInstancePtr);
 }
 
 NS_IMETHODIMP
 nsErrorService::RegisterErrorStringBundle(int16_t errorModule, const char *stringBundleURL)
 {
     return mErrorStringBundleURLMap.Put(errorModule, stringBundleURL);
 }
 
--- a/xpcom/ds/nsAtomTable.cpp
+++ b/xpcom/ds/nsAtomTable.cpp
@@ -549,21 +549,16 @@ RegisterStaticAtoms(const nsStaticAtom* 
 {
   // this does three things:
   // 1) wraps each static atom in a wrapper, if necessary
   // 2) initializes the address pointed to by each mBits slot
   // 3) puts the atom into the static atom table as well
   
   if (!gStaticAtomTable && !gStaticAtomTableSealed) {
     gStaticAtomTable = new nsDataHashtable<nsStringHashKey, nsIAtom*>();
-    if (!gStaticAtomTable) {
-      delete gStaticAtomTable;
-      gStaticAtomTable = nullptr;
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
     gStaticAtomTable->Init();
   }
   
   for (uint32_t i=0; i<aAtomCount; i++) {
 #ifdef NS_STATIC_ATOM_USE_WIDE_STRINGS
     NS_ASSERTION(nsCRT::IsAscii((PRUnichar*)aAtoms[i].mStringBuffer->Data()),
                  "Static atoms must be ASCII!");
 
--- a/xpcom/ds/nsByteBuffer.cpp
+++ b/xpcom/ds/nsByteBuffer.cpp
@@ -1,16 +1,17 @@
 /* -*- 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 "nsByteBuffer.h"
 #include "nsIInputStream.h"
 #include "nsCRT.h"
+#include "nsAutoPtr.h"
 
 #define MIN_BUFFER_SIZE 32
 
 ByteBufferImpl::ByteBufferImpl(void)
   : mBuffer(NULL), mSpace(0), mLength(0)
 {
 }
 
@@ -18,17 +19,17 @@ NS_IMETHODIMP
 ByteBufferImpl::Init(uint32_t aBufferSize)
 {
   if (aBufferSize < MIN_BUFFER_SIZE) {
     aBufferSize = MIN_BUFFER_SIZE;
   }
   mSpace = aBufferSize;
   mLength = 0;
   mBuffer = new char[aBufferSize];
-  return mBuffer ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
+  return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS1(ByteBufferImpl,nsIByteBuffer)
 
 ByteBufferImpl::~ByteBufferImpl()
 {
   if (nullptr != mBuffer) {
     delete[] mBuffer;
@@ -38,24 +39,19 @@ ByteBufferImpl::~ByteBufferImpl()
 }
 
 nsresult
 ByteBufferImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
 {
   if (aOuter)
     return NS_ERROR_NO_AGGREGATION;
 
-  ByteBufferImpl* it = new ByteBufferImpl();
-  if (nullptr == it) 
-    return NS_ERROR_OUT_OF_MEMORY;
+  nsRefPtr<ByteBufferImpl> it = new ByteBufferImpl();
 
-  NS_ADDREF(it);
-  nsresult rv = it->QueryInterface(aIID, (void**)aResult);
-  NS_RELEASE(it);
-  return rv;
+  return it->QueryInterface(aIID, (void**)aResult);
 }
 
 NS_IMETHODIMP_(uint32_t)
 ByteBufferImpl::GetLength(void) const
 {
   return mLength;
 }
 
@@ -73,25 +69,22 @@ ByteBufferImpl::GetBuffer(void) const
 
 NS_IMETHODIMP_(bool)
 ByteBufferImpl::Grow(uint32_t aNewSize)
 {
   if (aNewSize < MIN_BUFFER_SIZE) {
     aNewSize = MIN_BUFFER_SIZE;
   }
   char* newbuf = new char[aNewSize];
-  if (nullptr != newbuf) {
-    if (0 != mLength) {
-      memcpy(newbuf, mBuffer, mLength);
-    }
-    delete[] mBuffer;
-    mBuffer = newbuf;
-    return true;
+  if (0 != mLength) {
+    memcpy(newbuf, mBuffer, mLength);
   }
-  return false;
+  delete[] mBuffer;
+  mBuffer = newbuf;
+  return true;
 }
 
 NS_IMETHODIMP_(int32_t)
 ByteBufferImpl::Fill(nsresult* aErrorCode, nsIInputStream* aStream,
                      uint32_t aKeep)
 {
   NS_PRECONDITION(nullptr != aStream, "null stream");
   NS_PRECONDITION(aKeep <= mLength, "illegal keep count");
--- a/xpcom/ds/nsCheapSets.h
+++ b/xpcom/ds/nsCheapSets.h
@@ -104,19 +104,16 @@ nsCheapSet<EntryType>::Put(const KeyType
   switch (mState) {
   case ZERO:
     new (GetSingleEntry()) EntryType(EntryType::KeyToPointer(aVal));
     mState = ONE;
     return NS_OK;
   case ONE:
     {
       nsTHashtable<EntryType> *table = new nsTHashtable<EntryType>();
-      if (!table) {
-        return NS_ERROR_OUT_OF_MEMORY;
-      }
       table->Init();
       EntryType *entry = GetSingleEntry();
       table->PutEntry(entry->GetKey());
       entry->~EntryType();
       mUnion.table = table;
       mState = MANY;
     }
     // Fall through.
--- a/xpcom/ds/nsHashPropertyBag.cpp
+++ b/xpcom/ds/nsHashPropertyBag.cpp
@@ -11,29 +11,24 @@
 #include "nsIVariant.h"
 #include "nsIProperty.h"
 #include "nsVariant.h"
 #include "mozilla/Attributes.h"
 
 nsresult
 NS_NewHashPropertyBag(nsIWritablePropertyBag* *_retval)
 {
-    nsHashPropertyBag *hpb = new nsHashPropertyBag();
-    if (!hpb)
-        return NS_ERROR_OUT_OF_MEMORY;
-
-    NS_ADDREF(hpb);
+    nsRefPtr<nsHashPropertyBag> hpb = new nsHashPropertyBag();
 
     nsresult rv = hpb->Init();
     if (NS_FAILED(rv)) {
-        NS_RELEASE(hpb);
         return rv;
     }
 
-    *_retval = hpb;
+    hpb.forget(_retval);
     return NS_OK;
 }
 
 /*
  * nsHashPropertyBag impl
  */
 
 NS_IMPL_ADDREF(nsHashPropertyBag)
@@ -177,18 +172,16 @@ nsHashPropertyBag::GetPropertyAs ## Name
         return NS_ERROR_NOT_AVAILABLE; \
     return v->GetAs ## Name(_retval); \
 } \
 \
 NS_IMETHODIMP \
 nsHashPropertyBag::SetPropertyAs ## Name (const nsAString & prop, Type value) \
 { \
     nsCOMPtr<nsIWritableVariant> var = new nsVariant(); \
-    if (!var) \
-        return NS_ERROR_OUT_OF_MEMORY; \
     var->SetAs ## Name(value); \
     return SetProperty(prop, var); \
 }
 
 IMPL_GETSETPROPERTY_AS(Int32, int32_t)
 IMPL_GETSETPROPERTY_AS(Uint32, uint32_t)
 IMPL_GETSETPROPERTY_AS(Int64, int64_t)
 IMPL_GETSETPROPERTY_AS(Uint64, uint64_t)
@@ -242,44 +235,36 @@ nsHashPropertyBag::GetPropertyAsInterfac
     }
     return val->QueryInterface(aIID, _retval);
 }
 
 NS_IMETHODIMP
 nsHashPropertyBag::SetPropertyAsAString(const nsAString & prop, const nsAString & value)
 {
     nsCOMPtr<nsIWritableVariant> var = new nsVariant();
-    if (!var)
-        return NS_ERROR_OUT_OF_MEMORY;
     var->SetAsAString(value);
     return SetProperty(prop, var);
 }
 
 NS_IMETHODIMP
 nsHashPropertyBag::SetPropertyAsACString(const nsAString & prop, const nsACString & value)
 {
     nsCOMPtr<nsIWritableVariant> var = new nsVariant();
-    if (!var)
-        return NS_ERROR_OUT_OF_MEMORY;
     var->SetAsACString(value);
     return SetProperty(prop, var);
 }
 
 NS_IMETHODIMP
 nsHashPropertyBag::SetPropertyAsAUTF8String(const nsAString & prop, const nsACString & value)
 {
     nsCOMPtr<nsIWritableVariant> var = new nsVariant();
-    if (!var)
-        return NS_ERROR_OUT_OF_MEMORY;
     var->SetAsAUTF8String(value);
     return SetProperty(prop, var);
 }
 
 NS_IMETHODIMP
 nsHashPropertyBag::SetPropertyAsInterface(const nsAString & prop, nsISupports* value)
 {
     nsCOMPtr<nsIWritableVariant> var = new nsVariant();
-    if (!var)
-        return NS_ERROR_OUT_OF_MEMORY;
     var->SetAsISupports(value);
     return SetProperty(prop, var);
 }
 
--- a/xpcom/ds/nsObserverList.cpp
+++ b/xpcom/ds/nsObserverList.cpp
@@ -51,20 +51,17 @@ nsObserverList::RemoveObserver(nsIObserv
 
     return NS_OK;
 }
 
 nsresult
 nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
 {
     nsRefPtr<nsObserverEnumerator> e(new nsObserverEnumerator(this));
-    if (!e)
-        return NS_ERROR_OUT_OF_MEMORY;
-
-    NS_ADDREF(*anEnumerator = e);
+    e.forget(anEnumerator);
     return NS_OK;
 }
 
 void
 nsObserverList::FillObserverArray(nsCOMArray<nsIObserver> &aArray)
 {
     aArray.SetCapacity(mObservers.Length());
 
--- a/xpcom/ds/nsUnicharBuffer.cpp
+++ b/xpcom/ds/nsUnicharBuffer.cpp
@@ -1,49 +1,45 @@
 /* -*- 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 "nsUnicharBuffer.h"
 #include "nsCRT.h"
+#include "nsAutoPtr.h"
 
 #define MIN_BUFFER_SIZE 32
 
 UnicharBufferImpl::UnicharBufferImpl()
   : mBuffer(NULL), mSpace(0), mLength(0)
 {
 }
 
 NS_METHOD
 UnicharBufferImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
 {
   if (aOuter)
     return NS_ERROR_NO_AGGREGATION;
 
-  UnicharBufferImpl* it = new UnicharBufferImpl();
-  if (it == nullptr) 
-    return NS_ERROR_OUT_OF_MEMORY;
+  nsRefPtr<UnicharBufferImpl> it = new UnicharBufferImpl();
 
-  NS_ADDREF(it);
-  nsresult rv = it->QueryInterface(aIID, aResult);
-  NS_RELEASE(it);
-  return rv;
+  return it->QueryInterface(aIID, aResult);
 }
 
 NS_IMETHODIMP
 UnicharBufferImpl::Init(uint32_t aBufferSize)
 {
   if (aBufferSize < MIN_BUFFER_SIZE) {
     aBufferSize = MIN_BUFFER_SIZE;
   }
   mSpace = aBufferSize;
   mLength = 0;
   mBuffer = new PRUnichar[aBufferSize];
-  return mBuffer ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
+  return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS1(UnicharBufferImpl, nsIUnicharBuffer)
 
 UnicharBufferImpl::~UnicharBufferImpl()
 {
   if (nullptr != mBuffer) {
     delete[] mBuffer;
--- a/xpcom/ds/nsWindowsRegKey.cpp
+++ b/xpcom/ds/nsWindowsRegKey.cpp
@@ -6,16 +6,17 @@
 
 #include <windows.h>
 #include <shlwapi.h>
 #include <stdlib.h>
 #include "nsWindowsRegKey.h"
 #include "nsString.h"
 #include "nsCOMPtr.h"
 #include "mozilla/Attributes.h"
+#include "nsAutoPtr.h"
 
 //-----------------------------------------------------------------------------
 
 // According to MSDN, the following limits apply (in characters excluding room
 // for terminating null character):
 #define MAX_KEY_NAME_LEN     255
 #define MAX_VALUE_NAME_LEN   16383
 
@@ -100,36 +101,32 @@ nsWindowsRegKey::Create(uint32_t rootKey
 
 NS_IMETHODIMP
 nsWindowsRegKey::OpenChild(const nsAString &path, uint32_t mode,
                            nsIWindowsRegKey **result)
 {
   NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
 
   nsCOMPtr<nsIWindowsRegKey> child = new nsWindowsRegKey();
-  if (!child)
-    return NS_ERROR_OUT_OF_MEMORY;
   
   nsresult rv = child->Open((uintptr_t) mKey, path, mode);
   if (NS_FAILED(rv))
     return rv;
 
   child.swap(*result);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsWindowsRegKey::CreateChild(const nsAString &path, uint32_t mode,
                              nsIWindowsRegKey **result)
 {
   NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
 
   nsCOMPtr<nsIWindowsRegKey> child = new nsWindowsRegKey();
-  if (!child)
-    return NS_ERROR_OUT_OF_MEMORY;
   
   nsresult rv = child->Create((uintptr_t) mKey, path, mode);
   if (NS_FAILED(rv))
     return rv;
 
   child.swap(*result);
   return NS_OK;
 }
@@ -488,21 +485,18 @@ nsWindowsRegKey::IsWatching(bool *result
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 
 nsresult
 NS_NewWindowsRegKey(nsIWindowsRegKey **result)
 {
-  *result = new nsWindowsRegKey();
-  if (!*result)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(*result);
+  nsRefPtr<WindowsRegKey> key = new nsWindowsRegKey();
+  key.forget(result);
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 
 nsresult
 nsWindowsRegKeyConstructor(nsISupports *delegate, const nsIID &iid,
                            void **result)
--- a/xpcom/io/nsLocalFileUnix.cpp
+++ b/xpcom/io/nsLocalFileUnix.cpp
@@ -202,25 +202,22 @@ nsDirEnumeratorUnix::GetNextFile(nsIFile
 {
     nsresult rv;
     if (!mDir || !mEntry) {
         *_retval = nullptr;
         return NS_OK;
     }
 
     nsCOMPtr<nsIFile> file = new nsLocalFile();
-    if (!file)
-        return NS_ERROR_OUT_OF_MEMORY;
 
     if (NS_FAILED(rv = file->InitWithNativePath(mParentPath)) ||
         NS_FAILED(rv = file->AppendNative(nsDependentCString(mEntry->d_name))))
         return rv;
 
-    *_retval = file;
-    NS_ADDREF(*_retval);
+    file.forget(_retval);
     return GetNextEntry();
 }
 
 NS_IMETHODIMP 
 nsDirEnumeratorUnix::Close()
 {
     if (mDir) {
         closedir(mDir);
@@ -257,18 +254,16 @@ nsLocalFile::nsLocalFileConstructor(nsIS
                                     void **aInstancePtr)
 {
     NS_ENSURE_ARG_POINTER(aInstancePtr);
     NS_ENSURE_NO_AGGREGATION(outer);
 
     *aInstancePtr = nullptr;
 
     nsCOMPtr<nsIFile> inst = new nsLocalFile();
-    if (!inst)
-        return NS_ERROR_OUT_OF_MEMORY;
     return inst->QueryInterface(aIID, aInstancePtr);
 }
 
 bool 
 nsLocalFile::FillStatCache() {
     if (STAT(mPath.get(), &mCachedStat) == -1) {
         // try lstat it may be a symlink
         if (LSTAT(mPath.get(), &mCachedStat) == -1) {
@@ -277,22 +272,18 @@ nsLocalFile::FillStatCache() {
     }
     return true;
 }
 
 NS_IMETHODIMP
 nsLocalFile::Clone(nsIFile **file)
 {
     // Just copy-construct ourselves
-    *file = new nsLocalFile(*this);
-    if (!*file)
-      return NS_ERROR_OUT_OF_MEMORY;
-
-    NS_ADDREF(*file);
-    
+    nsRefPtr<nsLocalFile> copy = new nsLocalFile(*this);
+    copy.forget(file);
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLocalFile::InitWithNativePath(const nsACString &filePath)
 {
     if (filePath.Equals("~") || Substring(filePath, 0, 2).EqualsLiteral("~/")) {
         nsCOMPtr<nsIFile> homeDir;
@@ -924,18 +915,16 @@ nsLocalFile::Remove(bool recursive)
     if (NS_FAILED(rv))
         return rv;
 
     if (isSymLink || !S_ISDIR(mCachedStat.st_mode))
         return NSRESULT_FOR_RETURN(unlink(mPath.get()));
 
     if (recursive) {
         nsDirEnumeratorUnix *dir = new nsDirEnumeratorUnix();
-        if (!dir)
-            return NS_ERROR_OUT_OF_MEMORY;
 
         nsCOMPtr<nsISimpleEnumerator> dirRef(dir); // release on exit
 
         rv = dir->Init(this, false);
         if (NS_FAILED(rv))
             return rv;
 
         bool more;
@@ -1651,27 +1640,23 @@ NS_IMETHODIMP
 nsLocalFile::SetFollowLinks(bool aFollowLinks)
 {
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator **entries)
 {
-    nsDirEnumeratorUnix *dir = new nsDirEnumeratorUnix();
-    if (!dir)
-        return NS_ERROR_OUT_OF_MEMORY;
+    nsRefPtr<nsDirEnumeratorUnix> dir = new nsDirEnumeratorUnix();
 
-    NS_ADDREF(dir);
     nsresult rv = dir->Init(this, false);
     if (NS_FAILED(rv)) {
         *entries = nullptr;
-        NS_RELEASE(dir);
     } else {
-        *entries = dir; // transfer reference
+        dir.forget(entries);
     }
 
     return rv;
 }
 
 NS_IMETHODIMP
 nsLocalFile::Load(PRLibrary **_retval)
 {
@@ -1866,31 +1851,27 @@ nsLocalFile::Launch()
 #else
     return NS_ERROR_FAILURE;
 #endif
 }
 
 nsresult
 NS_NewNativeLocalFile(const nsACString &path, bool followSymlinks, nsIFile **result)
 {
-    nsLocalFile *file = new nsLocalFile();
-    if (!file)
-        return NS_ERROR_OUT_OF_MEMORY;
-    NS_ADDREF(file);
+    nsRefPtr<nsLocalFile> file = new nsLocalFile();
 
     file->SetFollowLinks(followSymlinks);
 
     if (!path.IsEmpty()) {
         nsresult rv = file->InitWithNativePath(path);
         if (NS_FAILED(rv)) {
-            NS_RELEASE(file);
             return rv;
         }
     }
-    *result = file;
+    file.forget(result);
     return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 // unicode support
 //-----------------------------------------------------------------------------
 
 #define SET_UCS(func, ucsArg) \
@@ -2435,44 +2416,36 @@ NS_IMETHODIMP nsLocalFile::InitWithFile(
     return rv;
 
   return InitWithNativePath(nativePath);
 }
 
 nsresult
 NS_NewLocalFileWithFSRef(const FSRef* aFSRef, bool aFollowLinks, nsILocalFileMac** result)
 {
-  nsLocalFile* file = new nsLocalFile();
-  if (file == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-  NS_ADDREF(file);
+  nsRefPtr<nsLocalFile> file = new nsLocalFile();
 
   file->SetFollowLinks(aFollowLinks);
 
   nsresult rv = file->InitWithFSRef(aFSRef);
   if (NS_FAILED(rv)) {
-    NS_RELEASE(file);
     return rv;
   }
-  *result = file;
+  file.forget(result);
   return NS_OK;
 }
 
 nsresult
 NS_NewLocalFileWithCFURL(const CFURLRef aURL, bool aFollowLinks, nsILocalFileMac** result)
 {
-  nsLocalFile* file = new nsLocalFile();
-  if (!file)
-    return NS_ERROR_OUT_OF_MEMORY;
-  NS_ADDREF(file);
+  nsRefPtr<nsLocalFile> file = new nsLocalFile();
 
   file->SetFollowLinks(aFollowLinks);
 
   nsresult rv = file->InitWithCFURL(aURL);
   if (NS_FAILED(rv)) {
-    NS_RELEASE(file);
     return rv;
   }
-  *result = file;
+  file.forget(result);
   return NS_OK;
 }
 
 #endif