Cleaning up the resources in Sharepoint Programming

Here are some tips for SharePoint programming for Cleaning up the resources :-
Objects that implement IDisposable

SPSite mySite= new SPSite(“http://server”) ;
……..
mySite.Dispose();



Some objects are created without “New” operator (Hidden)
SPSite mySite= new SPSite(“http://server”) ;
string str= mySite.RootWeb.Url;
……..
mySite.RootWeb.Dispose();
mySite.Dispose()


Some objects don’t need to be disposed
SPGroupsGrp= spWeb.AssociatedMemberGroup;

Don’t call Dispose/Close on a shared resource
SPSite site = SPControl.GetContextSite(this.Context);
SPWeb web = SPControl.GetContextWeb(this.Context);
SPSite mySite= SPContext.Current.Site;
SPWeb myWeb= SPContext.Current.Web;

No comments: