3

I need a list of SharePoint objects which need to be disposed.

This is for code analysis purpose. I have got a code review today and want a quick list of the objects to look out for in the code.

UPDATE Here is what to dispose and what not to dispose, if there is anything more to add then kindly advise:-

Dispose the SPWeb obtained by iterating over the Site.AllWebs collection

SPC110201: Dispose created SPSite objects

SPC110203: Dispose SPWeb objects created with SPSite.OpenWeb

SPC110204: Dispose SPSite objects created with SPSite.SelfServiceCreateSite

SPC110205: Dispose SPWeb created by SPLimitedWebPartManager

SPC110211: Dispose SPSite created by SPSiteCollection.Add

SPC110212: Dispose SPSite created by SPSiteCollection[] index operator

SPC110213: Dispose SPWeb created by SPWebCollection.Add

SPC110214: Dispose SPWeb created by SPWebCollection[] index operator

SPC110231: Do not dispose SPListEventProperties.Web

SPC110232: Do not dispose SPWebEventProperties.Web

SPC110233: Do not dispose SPItemEventProperties.Web

SPC110234: Do not dispose SPItemEventProperties.ListItem.Web

SPC110235: Do not dispose SPItemEventProperties.Web.Site

SPC110236: Do not dispose SPItemEventProperties.ListItem.Web.Site

SPC110237: Do not dispose SPFeatureReceiverProperties.Feature.Parent

SPC110241: Do not dispose SPSite.RootWeb

SPC110242: Do not dispose SPWeb.ParentWeb

SPC110243: Do not dispose SPList.ParentWeb

SPC110244: Do not dispose SPSite returned by SPControl.GetContextSite

SPC110245: Do not dispose SPWeb returned by SPControl.GetContextWeb

SPC110246: Do not dispose SPSite returned by SPContext.Current.Site

SPC110247: Do not dispose SPWeb returned by SPContext.Current.Web

SPC110261: Close PublishingWeb created by PublishingWeb.GetPublishingWebs[] index operator

SPC110262: Close PublishingWeb created by PublishingWeb.GetVariation

SPC110263: Close PublishingWeb created by PublishingWebCollection.Add

SPC110281: Dispose SPSite created with UserProfiles.PersonalSite

Reference: http://docs.spcaf.com/v4/SPCAF_VISITORGROUP_RULES_MEMORYDISPOSALGROUP.html

3 Answers 3

2
SPWeb
SPSite

The web in SPLimitedWebPartManager 
The site in UserProfiles.PersonalSite

Also for publishing webs, closing:

Close PublishingWeb created by PublishingWeb.GetPublishingWebs[] index operator
Close PublishingWeb created by PublishingWeb.GetVariation
Close PublishingWeb created by PublishingWebCollection.Add

from http://docs.spcaf.com/v4/SPCAF_VISITORGROUP_RULES_MEMORYDISPOSALGROUP.html

Dispose guidelines: http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx

What not to dispose : http://ankursharepoint.blogspot.com/2012/11/sharepoint-2010-do-not-dispose-guidance.html

More great information: http://blogs.msdn.com/b/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx

3
  • 1
    docs.spcaf.com/v4/… Add this to the list
    – variable
    Apr 10, 2014 at 5:48
  • According to link from @variable one should also dispose of PublishingWeb in some cases (in addition to SPWeb)
    – eirikb
    Apr 10, 2014 at 6:06
  • Added those to the list. Apr 10, 2014 at 6:23
2

Here is what to dispose and what not to dispose:-

Dispose the SPWeb obtained by iterating over the Site.AllWebs collection

SPC110201: Dispose created SPSite objects

SPC110203: Dispose SPWeb objects created with SPSite.OpenWeb

SPC110204: Dispose SPSite objects created with SPSite.SelfServiceCreateSite

SPC110205: Dispose SPWeb created by SPLimitedWebPartManager

SPC110211: Dispose SPSite created by SPSiteCollection.Add

SPC110212: Dispose SPSite created by SPSiteCollection[] index operator

SPC110213: Dispose SPWeb created by SPWebCollection.Add

SPC110214: Dispose SPWeb created by SPWebCollection[] index operator

SPC110231: Do not dispose SPListEventProperties.Web

SPC110232: Do not dispose SPWebEventProperties.Web

SPC110233: Do not dispose SPItemEventProperties.Web

SPC110234: Do not dispose SPItemEventProperties.ListItem.Web

SPC110235: Do not dispose SPItemEventProperties.Web.Site

SPC110236: Do not dispose SPItemEventProperties.ListItem.Web.Site

SPC110237: Do not dispose SPFeatureReceiverProperties.Feature.Parent

SPC110241: Do not dispose SPSite.RootWeb

SPC110242: Do not dispose SPWeb.ParentWeb

SPC110243: Do not dispose SPList.ParentWeb

SPC110244: Do not dispose SPSite returned by SPControl.GetContextSite

SPC110245: Do not dispose SPWeb returned by SPControl.GetContextWeb

SPC110246: Do not dispose SPSite returned by SPContext.Current.Site

SPC110247: Do not dispose SPWeb returned by SPContext.Current.Web

SPC110261: Close PublishingWeb created by PublishingWeb.GetPublishingWebs[] index operator

SPC110262: Close PublishingWeb created by PublishingWeb.GetVariation

SPC110263: Close PublishingWeb created by PublishingWebCollection.Add

SPC110281: Dispose SPSite created with UserProfiles.PersonalSite

Reference: http://docs.spcaf.com/v4/SPCAF_VISITORGROUP_RULES_MEMORYDISPOSALGROUP.html

1

I want to add to the two lists above

Dispose the SPWeb obtained by iterating over the Site.AllWebs collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.