From 10f8d0856d9c694e0fe31cdadb3a7981a9a62e7a Mon Sep 17 00:00:00 2001 From: "sfraser%netscape.com" Date: Sat, 19 Jun 1999 22:06:35 +0000 Subject: [PATCH] Muchos leak fixing. CreateNewTopLevel window returns an AddReffed window. You must release it. Also use nsCOMPtr in a couple of other places. git-svn-id: svn://10.0.0.236/trunk@35928 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/docshell/base/nsWebShell.cpp | 62 ++++++++++++++++++---------- mozilla/webshell/src/nsWebShell.cpp | 62 ++++++++++++++++++---------- 2 files changed, 82 insertions(+), 42 deletions(-) diff --git a/mozilla/docshell/base/nsWebShell.cpp b/mozilla/docshell/base/nsWebShell.cpp index 8d6852767db..89b3bcb4eb5 100644 --- a/mozilla/docshell/base/nsWebShell.cpp +++ b/mozilla/docshell/base/nsWebShell.cpp @@ -665,8 +665,24 @@ nsWebShell::DestroyChildren() mChildren.Clear(); } -NS_IMPL_THREADSAFE_ADDREF(nsWebShell) -NS_IMPL_THREADSAFE_RELEASE(nsWebShell) + +NS_IMETHODIMP_(nsrefcnt) nsWebShell::AddRef(void) +{ + NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt"); + ++mRefCnt; + return mRefCnt; +} + +NS_IMETHODIMP_(nsrefcnt) nsWebShell::Release(void) +{ + NS_PRECONDITION(0 != mRefCnt, "dup release"); + --mRefCnt; + if (mRefCnt == 0) { + NS_DELETEXPCOM(this); + return 0; + } + return mRefCnt; +} nsresult nsWebShell::QueryInterface(REFNSIID aIID, void** aInstancePtr) @@ -1764,21 +1780,22 @@ nsWebShell::LoadURL(const PRUnichar *aURLSpec, mURL = urlSpec.GetUnicode(); - - nsISessionHistory * shist; - + /* If this is one of the frames, get it from the top level shell */ if (aModifyHistory) { - nsIWebShell * ws; - GetRootWebShell(ws); - if (ws) - ws->GetSessionHistory(shist); - /* Add yourself to the Session History */ - if (shist) { - PRInt32 ret=0; - ret = shist->add(this); - } + nsCOMPtr webShell; + GetRootWebShell(*getter_AddRefs(webShell)); + if (webShell) + { + nsCOMPtr shist; + webShell->GetSessionHistory(*getter_AddRefs(shist)); + /* Add yourself to the Session History */ + if (shist) { + PRInt32 ret=0; + ret = shist->add(this); + } + } } @@ -2311,7 +2328,7 @@ nsWebShell::GetTarget(const PRUnichar* aName) NS_ADDREF(target); } else if (name.EqualsIgnoreCase("_top")) { - GetRootWebShell(target); + GetRootWebShell(target); // this addrefs, which is OK } else { // Look from the top of the tree downward @@ -2634,23 +2651,26 @@ nsWebShell::OnEndDocumentLoad(nsIDocumentLoader* loader, } } - nsIDocumentLoaderObserver * dlObserver = nsnull; + nsCOMPtr dlObserver; if (!mDocLoaderObserver && mParent) { /* If this is a frame (in which case it would have a parent && doesn't * have a documentloaderObserver, get it from the rootWebShell */ - nsIWebShell * root=nsnull; - GetRootWebShell(root); + nsCOMPtr root; + GetRootWebShell(*getter_AddRefs(root)); if (root) - root->GetDocLoaderObserver(dlObserver); + root->GetDocLoaderObserver(*getter_AddRefs(dlObserver)); } else - dlObserver = mDocLoaderObserver; + { + dlObserver = do_QueryInterface(mDocLoaderObserver); // we need this to addref + } + /* * Fire the OnEndDocumentLoad of the DocLoaderobserver */ - if ((nsnull != dlObserver) && (nsnull != aURL)){ + if (dlObserver && (nsnull != aURL)) { dlObserver->OnEndDocumentLoad(mDocLoader, aURL, aStatus, aWebShell); } diff --git a/mozilla/webshell/src/nsWebShell.cpp b/mozilla/webshell/src/nsWebShell.cpp index 8d6852767db..89b3bcb4eb5 100644 --- a/mozilla/webshell/src/nsWebShell.cpp +++ b/mozilla/webshell/src/nsWebShell.cpp @@ -665,8 +665,24 @@ nsWebShell::DestroyChildren() mChildren.Clear(); } -NS_IMPL_THREADSAFE_ADDREF(nsWebShell) -NS_IMPL_THREADSAFE_RELEASE(nsWebShell) + +NS_IMETHODIMP_(nsrefcnt) nsWebShell::AddRef(void) +{ + NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt"); + ++mRefCnt; + return mRefCnt; +} + +NS_IMETHODIMP_(nsrefcnt) nsWebShell::Release(void) +{ + NS_PRECONDITION(0 != mRefCnt, "dup release"); + --mRefCnt; + if (mRefCnt == 0) { + NS_DELETEXPCOM(this); + return 0; + } + return mRefCnt; +} nsresult nsWebShell::QueryInterface(REFNSIID aIID, void** aInstancePtr) @@ -1764,21 +1780,22 @@ nsWebShell::LoadURL(const PRUnichar *aURLSpec, mURL = urlSpec.GetUnicode(); - - nsISessionHistory * shist; - + /* If this is one of the frames, get it from the top level shell */ if (aModifyHistory) { - nsIWebShell * ws; - GetRootWebShell(ws); - if (ws) - ws->GetSessionHistory(shist); - /* Add yourself to the Session History */ - if (shist) { - PRInt32 ret=0; - ret = shist->add(this); - } + nsCOMPtr webShell; + GetRootWebShell(*getter_AddRefs(webShell)); + if (webShell) + { + nsCOMPtr shist; + webShell->GetSessionHistory(*getter_AddRefs(shist)); + /* Add yourself to the Session History */ + if (shist) { + PRInt32 ret=0; + ret = shist->add(this); + } + } } @@ -2311,7 +2328,7 @@ nsWebShell::GetTarget(const PRUnichar* aName) NS_ADDREF(target); } else if (name.EqualsIgnoreCase("_top")) { - GetRootWebShell(target); + GetRootWebShell(target); // this addrefs, which is OK } else { // Look from the top of the tree downward @@ -2634,23 +2651,26 @@ nsWebShell::OnEndDocumentLoad(nsIDocumentLoader* loader, } } - nsIDocumentLoaderObserver * dlObserver = nsnull; + nsCOMPtr dlObserver; if (!mDocLoaderObserver && mParent) { /* If this is a frame (in which case it would have a parent && doesn't * have a documentloaderObserver, get it from the rootWebShell */ - nsIWebShell * root=nsnull; - GetRootWebShell(root); + nsCOMPtr root; + GetRootWebShell(*getter_AddRefs(root)); if (root) - root->GetDocLoaderObserver(dlObserver); + root->GetDocLoaderObserver(*getter_AddRefs(dlObserver)); } else - dlObserver = mDocLoaderObserver; + { + dlObserver = do_QueryInterface(mDocLoaderObserver); // we need this to addref + } + /* * Fire the OnEndDocumentLoad of the DocLoaderobserver */ - if ((nsnull != dlObserver) && (nsnull != aURL)){ + if (dlObserver && (nsnull != aURL)) { dlObserver->OnEndDocumentLoad(mDocLoader, aURL, aStatus, aWebShell); }