SharePoint 2007/2010 “Do Not Dispose Guidance” + SPDisposeCheck


Below I put together a quick reference of the known SharePoint properties and methods that you do not want to call Dispose against when developing SharePoint applications (AKA “Do Not Dispose Guidance”).  Please check back on this blog post as I will be keeping it updated as we move closer to the SharePoint Server 2010 Release second half of 2010.  I am communicating directly with the SharePoint Product Team and plan on validating the existing guidance for Microsoft Office SharePoint Server 2007.  If you have additional questions on items you don’t see here please feel free to leave comments on this blog with code samples.

SPDisposeCheck: The current version v1.3.1 (January 2009) of SPDisposeCheck utility does not scan for “Do Not Dispose Guidance”.  SPDisposeCheck does a great job drawing your attention to areas of your source code that potentially do not conform with our (Microsoft’s) guidance on Disposing SharePoint objects but you might not be fully aware that it’s not going to report any of the “Do Not Dispose Guidance” listed below.  Until we release the refreshed version of SPDisposeCheck that targets Do Not Dispose Guidance” you will need to manually review the source code for the guidance on my blog and our Dispose WhitePaper.  The current version of SPDisposeCheck was targeted and tested against Microsoft Office SharePoint Server 2007 but we are working on a update that works with SharePoint Server 2010 Beta 2.

Remember it’s best practice to regularly review your ULS logs for Dispose() related leaks that have occurred during runtime.. SharePoint Server internally is instrumented to report on Dispose related leaks.

“Do Not Dispose Guidance” as of 11/30/2009:

SharePoint 2007 & 2010:

  • SPContext.Current.Site
  • SPContext.Current.Web
  • SPContext.Site
  • SPContext.Web
  • SPControl.GetContextWeb(..)
  • SPControl.GetContextSite(..)
  • SPFeatureReceiverProperties.Feature.Parent
  • SPItemEventProperties.ListItem.Web
  • SPList.BreakRoleInheritance()
    • Do not call list.ParentWeb.Dispose()
  • SPListEventProperties.Web
  • SPListEventProperties.List.Web
  • SPSite.RootWeb
    • Problems may occur when SPContext.Web has equality to the SPContext.Web.. make sure you dispose of SPSite and it will cleanup sub webs automatically
  • SPSite.LockIssue
  • SPSite.Owner
  • SPSite.SecondaryContact
  • SPWeb.ParentWeb
  • SPWebEventProperties.Web

Changes:

  • Microsoft.SharePoint.WebControls.SiteAdminsitrationSelector.CurrentItem
    • When used with WSS 3.0 you must call Dispose(), with SharePoint Foundation 2010 you don’t.
  • Event Receivers and properties.OpenWeb()
    • WSS 3.0: When you call properties.OpenWeb() the returned SPWeb will need to call Dispose()
    • SharePoint Foundation 2010: Use the newly introduced SPItemEventProperties.Web property instead of SPItemEventProperties.OpenWeb() for better performance and to avoid the need to call Dispose().

Comments (7)

  1. sankeeth says:

    hi, is there any update version "SPDisposeCheck" for SP 2010 Server.. please let me know… thanks

  2. Patrick says:

    I've got the same question as sankeeth, can you please answer this?

  3. badgej says:

    Roger,

    What guidance do you have on disposing SPWeb.ParentWeb when it is accessed multiple levels depp? This would be like: SPContext.Curent.Web.ParentWeb.ParentWeb . Obviously, this wouldn't happen in code normally – but happens when you recursively get ParentWebs of a passed Web object. Would these ParentWebs also fall under the "Do not dispose" guidance?

  4. Roger Lamb says:

    badgej: There is no need to dispose ParentWeb at any level.

  5. Michael says:

    Does this go for using statements as well?

  6. Mehul Bhuva says:

    Do I need to CLOSE the Publishing Site object, for eg:

    PublishingSite psSite = new PublishingSite(site.Url);

    psSite.Close();