Pages

June 26, 2012

Quick guide to dispose objects in SharePoint




Ever wanted a quick reference on what to or not to dispose in SharePoint?
This is the my idea about such a guide.

How to dispose?
You can use the Dispose() method, or the Using keyword context

  
What to dispose?
to ease reading, in general: Dispose red, keep green!.

Dispose when you have: new, openWeballWebs,Webs

Do NOT dispose when you have: SPContext, RootWeb, ParentWeb

Also from latest version of SPDisposeCheck tool, this is a reference list of Do Not Dispose Rules checked:
SPListEventProperties.Web
SPWebEventProperties.Web
SPItemEventProperties.Web
SPItemEventProperties.ListItem.Web
SPItemEventProperties.Web.Site
SPItemEventProperties.ListItem.Web.Site
SPFeatureReceiverProperties.Feature.Parent
SPList.ParentWeb&SPListEventProperties.List.Web
SPWeb.ParentWeb
SPContext.Site&SPContext.Current.Site
SPContext.Web&SPContext.Current.Web
SPSite.RootWeb
SPControl.GetContextWeb(..)
SPControl.GetContextSite(..)
Microsoft.SharePoint.Administration.SPSiteAdministration.OpenWeb()
Microsoft.SharePoint.SPItemEventProperties.OpenWeb()

You can always check the latest version of SPDisposeCheck utility at: http://archive.msdn.microsoft.com/SPDisposeCheck


1 comment: