From 6541dcc119ef5bd82e8ae0b58bab469d1a950a6d Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Tue, 1 Jul 2003 01:03:02 +0000 Subject: [PATCH] Fix some memory leaks in Inspector. Bug 211166, r=caillon, sr=alecf --- .../inspector/base/src/inFileSearch.cpp | 58 ++++++++----------- extensions/inspector/base/src/inFileSearch.h | 9 +-- .../inspector/base/src/inLayoutUtils.cpp | 2 +- extensions/inspector/base/src/inLayoutUtils.h | 2 +- 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/extensions/inspector/base/src/inFileSearch.cpp b/extensions/inspector/base/src/inFileSearch.cpp index 0da96386fee..6d924401db3 100644 --- a/extensions/inspector/base/src/inFileSearch.cpp +++ b/extensions/inspector/base/src/inFileSearch.cpp @@ -149,8 +149,8 @@ inFileSearch::SearchStop() NS_IMETHODIMP inFileSearch::SearchStep(PRBool* _retval) { - nsIFile* nextDir; - PRBool more = GetNextSubDirectory(&nextDir); + nsCOMPtr nextDir; + PRBool more = GetNextSubDirectory(getter_AddRefs(nextDir)); if (more) { SearchDirectory(nextDir, PR_FALSE); @@ -167,12 +167,12 @@ inFileSearch::GetStringResultAt(PRInt32 aIndex, nsAString& _retval) { nsCOMPtr file; - _retval = NS_LITERAL_STRING(""); + _retval.Truncate(); if (mHoldResults) { - nsCOMPtr supports; - mResults->GetElementAt(aIndex, getter_AddRefs(supports)); - file = do_QueryInterface(supports); + if (aIndex < mResults.Count()) { + file = mResults[aIndex]; + } } else if (aIndex == mResultCount-1 && mLastResult) { // get the path of the last result as an nsAutoString file = mLastResult; @@ -344,11 +344,10 @@ inFileSearch::GetCurrentDirectory(nsIFile** aCurrentDirectory) NS_IMETHODIMP inFileSearch::GetFileResultAt(PRInt32 aIndex, nsIFile** _retval) { - if (mHoldResults && mResults) { - nsCOMPtr supports; - mResults->GetElementAt(aIndex, getter_AddRefs(supports)); - nsCOMPtr file = do_QueryInterface(supports); - *_retval = file; + if (mHoldResults) { + if (aIndex < mResults.Count()) { + NS_IF_ADDREF(*_retval = mResults[aIndex]); + } } else if (aIndex == mResultCount-1 && mLastResult) { *_retval = mLastResult; NS_IF_ADDREF(*_retval); @@ -378,11 +377,7 @@ inFileSearch::GetSubDirectories(nsIFile* aDir, nsISupportsArray** _retval) nsresult inFileSearch::InitSearch() { - if (mHoldResults) { - mResults = do_CreateInstance("@mozilla.org/supports-array;1"); - } else { - mResults = nsnull; - } + mResults.Clear(); mLastResult = nsnull; mResultCount = 0; @@ -442,7 +437,7 @@ nsresult inFileSearch::PrepareResult(nsIFile* aFile, PRBool aIsSync) { if (aIsSync || mHoldResults) { - mResults->AppendElement(aFile); + mResults.AppendObject(aFile); } if (!aIsSync) { @@ -470,7 +465,7 @@ inFileSearch::InitSearchLoop() nsresult inFileSearch::InitSubDirectoryStack() { - mDirStack = do_CreateInstance("@mozilla.org/supports-array;1"); + mDirStack.Clear(); return NS_OK; } @@ -479,30 +474,25 @@ PRBool inFileSearch::GetNextSubDirectory(nsIFile** aDir) { // get the enumerator on top of the stack - nsCOMPtr supports; nsCOMPtr nextDirs; - PRUint32 count; - while (PR_TRUE) { - mDirStack->Count(&count); + PRInt32 count = mDirStack.Count(); // the stack is empty, so our search must be complete if (count == 0) return PR_FALSE; // get the next directory enumerator on the stack - mDirStack->GetElementAt(count-1, getter_AddRefs(supports)); - nextDirs = do_QueryInterface(supports); + nextDirs = mDirStack[count-1]; // get the next directory from the enumerator - nsIFile* dir = GetNextDirectory(nextDirs); + *aDir = GetNextDirectory(nextDirs).get(); - if (dir) { + if (*aDir) { // this enumerator is ready to rock, so let's move on - *aDir = dir; return PR_TRUE; - } else { - // enumerator is done, so pop it off the stack - mDirStack->RemoveElement(supports); } + + // enumerator is done, so pop it off the stack + mDirStack.RemoveObjectAt(count-1); } @@ -512,13 +502,13 @@ inFileSearch::GetNextSubDirectory(nsIFile** aDir) nsresult inFileSearch::PushSubDirectoryOnStack(nsIFile* aDir) { - nsISimpleEnumerator* entries; - aDir->GetDirectoryEntries(&entries); - mDirStack->AppendElement(entries); + nsCOMPtr entries; + aDir->GetDirectoryEntries(getter_AddRefs(entries)); + mDirStack.AppendObject(entries); return NS_OK; } -nsIFile* +already_AddRefed inFileSearch::GetNextDirectory(nsISimpleEnumerator* aEnum) { nsCOMPtr file; diff --git a/extensions/inspector/base/src/inFileSearch.h b/extensions/inspector/base/src/inFileSearch.h index 0440e9dba43..e2245e08f36 100644 --- a/extensions/inspector/base/src/inFileSearch.h +++ b/extensions/inspector/base/src/inFileSearch.h @@ -43,7 +43,8 @@ #include "nsString.h" #include "nsVoidArray.h" -#include "nsISupportsArray.h" +#include "nsISimpleEnumerator.h" +#include "nsCOMArray.h" #include "inISearchObserver.h" #include "nsIFile.h" #include "inSearchLoop.h" @@ -60,8 +61,8 @@ public: protected: nsCOMPtr mObserver; - nsCOMPtr mResults; - nsCOMPtr mDirStack; + nsCOMArray mResults; + nsCOMArray mDirStack; nsCOMPtr mLastResult; nsCOMPtr mSearchPath; inSearchLoop* mSearchLoop; @@ -87,7 +88,7 @@ protected: nsresult InitSubDirectoryStack(); PRBool GetNextSubDirectory(nsIFile** aDir); nsresult PushSubDirectoryOnStack(nsIFile* aDir); - nsIFile* GetNextDirectory(nsISimpleEnumerator* aEnum); + already_AddRefed GetNextDirectory(nsISimpleEnumerator* aEnum); // pattern matching PRBool MatchFile(nsIFile* aFile); diff --git a/extensions/inspector/base/src/inLayoutUtils.cpp b/extensions/inspector/base/src/inLayoutUtils.cpp index 4cb887a0fbd..5381dd95dbb 100644 --- a/extensions/inspector/base/src/inLayoutUtils.cpp +++ b/extensions/inspector/base/src/inLayoutUtils.cpp @@ -103,7 +103,7 @@ inLayoutUtils::GetFrameFor(nsIDOMElement* aElement, nsIPresShell* aShell) return frame; } -nsIRenderingContext* +already_AddRefed inLayoutUtils::GetRenderingContextFor(nsIPresShell* aShell) { nsCOMPtr viewman; diff --git a/extensions/inspector/base/src/inLayoutUtils.h b/extensions/inspector/base/src/inLayoutUtils.h index c4cdb5343fe..f8360c097bc 100644 --- a/extensions/inspector/base/src/inLayoutUtils.h +++ b/extensions/inspector/base/src/inLayoutUtils.h @@ -56,7 +56,7 @@ public: static nsIDOMWindowInternal* GetWindowFor(nsIDOMDocument* aDoc); static nsIPresShell* GetPresShellFor(nsISupports* aThing); static nsIFrame* GetFrameFor(nsIDOMElement* aElement, nsIPresShell* aShell); - static nsIRenderingContext* GetRenderingContextFor(nsIPresShell* aShell); + static already_AddRefed GetRenderingContextFor(nsIPresShell* aShell); static nsIEventStateManager* GetEventStateManagerFor(nsIDOMElement *aElement); static nsIBindingManager* GetBindingManagerFor(nsIDOMNode* aNode); static nsIDOMDocument* GetSubDocumentFor(nsIDOMNode* aNode);