Updated SPSite.RootWeb Dispose Guidance

I have recently been working directly with the Microsoft Office SharePoint Server 2007 (MOSS 2007) Product Group as well as several Subject Matter Experts in the Microsoft Services field to help clarify some Dispose edge cases which have been causing confusion which have surfaced in the MOSS Developer community.  Taking a closer look at the RootWeb Dispose Best Practice listed in the MSDN white paper Best Practices: Using Disposable Windows SharePoint Services Objects there is an new exception to the SPSite.RootWeb Dispose guidance.

Do not explicitly call Dispose() on the SPSite.RootWeb property.  The dispose cleanup will be handled automatically by the SharePoint and the .NET framework.  For existing SharePoint customizations removal of explicit RootWeb Dispose is recommended to avoid an edge case condition where the SPContext.Current.Web has equality to the SPSite.RootWeb.  Problems can occur when disposing RootWeb when obtained from any variation of SPContext (For Example: SPContext.Site.RootWeb, SPContext.Current.Site.RootWeb and GetContextSite(Context).RootWeb ).  Note the owning SPSite object must be properly Disposed (or not Disposed in the case of SPContext) as described here - SharePoint 2007 and WSS 3.0 Dispose Patterns by Example.

In addition, SPSite properties LockIssue, Owner, and SecondaryContact internally used the RootWeb property.  Based on the updated guidance for RootWeb no explicit Dispose on any of these properties are required.  Again, note the same dependency on properly Disposing the owning SPSite object is in effect.

 public void RootWebBestPractice()
{
    // Exception to "Best Practices: Using Disposable Windows SharePoint Services Objects" White paper

    // Example 1 - new SPSite
    using (SPSite siteCollection = new SPSite("https://moss"))
    {
        SPWeb rootWeb1 = siteCollection.RootWeb;
        // No explicit rootWeb1 dispose required
    }  // siteCollection automatically disposed by implementing using()
    // rootWeb1 will be Disposed by SPSite

    // Example 2 - SPContext and SPControl
    SPWeb rootWeb2 = SPContext.Current.Site.RootWeb;
    // Also would apply to SPControl.GetContextSite(Context);
    // No explicit rootWeb2 dispose required because it's obtained from SPContext.Current.Site
}