Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 2 down vote accepted
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

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

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

share|improve this answer

I want to add to the two lists above

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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