Spexception: Trying To Use An Spweb Object That Has Been Closed Or Disposed And Is No Longer Valid

Just about any developer working long enough withof the SPWeb object.
SharePoint will, at some point, wake up in the middleOther updates I found interesting (and frustrating)
of the night with the words "Memory Leak" echoingare:
in through their head... No - it is not the cat's fault,* Calling SPWeb.AllWebs collection requires you to
and no it does not mean you forgot to take out thedispose of this collection object.
trash again... What did happen? You came across one* Creating a new site by SPWeb.Webs.Add() - need
of Microsoft's "best practices" articles regardingto dispose of the web object returned.
disposing of SPWeb and SPSite objects in* Calling GetLimitedWebPartManager() method? It will
SharePoint...create a SPWeb object of its own and not dispose
According to the original MS guidance, every SPWebof it properly.
or SPSite object you create (or get) that was notPlease review the complete updated best practices
initiated within the page's context (i.e. notguide at MSDN here:
SPContext.Current.Web and notI hope Microsoft will release an update for this issue
SPControl.GetContextWeb(Context) ) must besoon.. Just thinking of all the code lines I wrote under
disposed of.the wrong best practices article is making me dizzy...
It used to be that the "using" statement constructGood luck and happy disposing!
was recommended to avoid forgetting to dispose... inShai Petel.
other cases you'd put up a flag and dispose of it at=~=~= Update =~=~=
the end of your code's life cycle - either way wasOk, this explains why it took me so long to find this
good enough.problem...
Being the "good guys" that we are, we went overI had this code in my web part, written according to
and updated all of our codes and components tothe original best practices:using(SPSite site =
match these guide lines.SPContext.Current.Web.Site)
And what happened? Every once in a while disposing{
these objects would cause baffling errors such as:    using(SPWeb web = site.RootWeb)
"Trying to use a SPWeb object that has been closed    {
or disposed and is no longer valid".        //Some code here...
But hey, if we call SPContext.Current.Site.RootWeb -    }
the best practices guidelines said we have to dispose}
of it! Same goes for SPContext.Current.Web.Site andI cannot explain this but I only got this error on some
SPContext.Current.Web.Site.RootWeb etc.sites, and on others it worked OK. Strange huh?
Recently I came across a great article by RogerWhat I found is that this code will throw exception
Lamb showing a few updates from Microsofton any team site I created within a site collection
regarding these disposable objects andthat is not in the web application root path (i.e. sites
recommendations.under
So - pay close attention to these updates... it justAnyway - according to the new guidelines, I should
might help you sleep through the night!not dispose of the RootWeb object in this case. If I
* When making a call todispose of the SPSite object - it disposes of the
SPContext.Current.Web.Site.RootWeb - you nowRootWeb object -in this case my context web
have to dispose only of the SPSite object:object...
SPContext.Current.Web.Site!So I updated my code to something like this and it
Basically, you should NOT dispose of theworked:
SPSite.RootWeb object directly.SPWeb web = SPContext.Current.Site.RootWeb;//No
* When calling SPWeb.ParentWeb - you should NOTdisposing
dispose of the ParentWeb object!//Some code here...
* Same goes to SPList.ParentWeb - do NOT dispose