sharepointdisposespwebspsite

Should I dispose of this SPWeb?


I'm hoping someone can help me out. I need to get the root web of the current site from the SPContext. It's easily done with the following

SPContext.Current.Site.RootWeb

I'm comfortable with the idea that the SPSite object here at SPContext.Current.Site.RootWeb shouldn't be disposed of, but what about the SPWeb object I'm getting from the SPSite. Will, when the SPSite get's disposed, the rootweb SPWeb get disposed to? Or do I need to dispose of it myself?


Solution

  • Calls to SPSite.RootWeb should not be disposed. Disposing the SPSite will also dispose the RootWeb.

    There is a bug in SPDisposeCheck where it flags if you do dispose it, and if you don't (damned either way!) I detailed how I solved this in this blog post, as you can't use an SPDisposeCheckIgnore attribute in elevated privileges blocks.