SPC110205: Dispose SPWeb created by SPLimitedWebPartManager

The SPLimitedWebPartManager class contains a reference to an internal SPWeb object that must be disposed.

TypeName: DisposeSPWebCreatedBySPLimitedWebPartManager
CheckId: SPC110205
Severity: Warning
Type: AssemblyFileReference
Resolution

Dispose any SPWeb object which has been created by SPLimitedWebPartManager. See sample from MSDN:

Bad Coding Practice

void SPLimitedWebPartManagerLeak()
{
  using (SPSite siteCollection = new SPSite("http://moss"))
  {
    using (SPWeb web = siteCollection.OpenWeb())
    {
      SPFile page = web.GetFile("Source_Folder_Name/Source_Page");
      SPLimitedWebPartManager webPartManager =
        page.GetLimitedWebPartManager(PersonalizationScope.Shared);
        // SPWeb object webPartManager.Web leaked.
    } // SPWeb object web.Dispose() automatically called.
  }  // SPSite object siteCollection.Dispose() automatically called.  
}

Good Coding Practice
void SPLimitedWebPartManagerLeak()
{
  using (SPSite siteCollection = new SPSite("http://moss"))
  {
    using (SPWeb web = siteCollection.OpenWeb())
    {
      SPFile page = web.GetFile("Source_Folder_Name/Source_Page");
      SPLimitedWebPartManager webPartManager =
        page.GetLimitedWebPartManager(PersonalizationScope.Shared);
        webPartManaber.Web.Dispose();
    } // SPWeb object web.Dispose() automatically called.
  }  // SPSite object siteCollection.Dispose() automatically called.  
}

Remarks

Rule relates to SPDisposeCheckId 'SPDisposeCheckID_160'. To ignore this rule add the attribute '[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_160, "Caller will dispose")]' to your method. Due to some limitations in current analysis the rule only checks for calls to the Web property of SPLimitedWebPartManager. Currently the rule cannot check if also Dispose is called on the property. This can lead to misleading warnings.

Links

comments powered by Disqus