To dispose, or not to dispose - That is the question

Posted on 9/28/2008 @ 2:41 AM in #SharePoint by | Feedback | 4075 views

Apparently, there is still plenty of confusion around what you should, or should not dispose in writing code for SharePoint.

Here are 3 rules to live by:

1) Do not dispose objects you didn't create. In other words, in a feature receiver, if "Parent" is SPWeb or SPSite - don't be callin' dispose on that. You didn't create that object, and after your feature receiver is done, parts of the SharePoint framework may still need that object. This rule applies to general .NET.

2) Dispose is like a pretty girl, if you see it, call it... but don't break rule #1. i.e. don't call a pretty girl that isn't confirmed unattached, .. if her large mammalian boyfriend finds out, he may knock your teeth out. This rule applies to general .NET as well.

3) If you call SPSite.Dispose, it will automatically call Dispose on all the contained SPWebs, thus unless you have an extremely borderline crazy logic that creates 200 SPWebs that you must dispose manually out of band, you should simply call SPSite.Dispose. This wasn't the case in WSS 2 mind you!

Sound off but keep it civil:

Older comments..


On 9/28/2008 3:13:42 AM Mika Berglund said ..
Microsoft has also published a couple of articles about coding best practices for SharePoint, including disposing of SharePoint objects.

You can find the articles here:


- http://msdn.microsoft.com/en-us/library/bb687949.aspx


- http://msdn.microsoft.com/en-us/library/aa973248.aspx

// Mika


On 9/28/2008 9:02:46 AM Jeremy Thake said ..

On 9/28/2008 12:22:41 PM Sahil Malik said ..
Thank you Jeremy. Y'know the reason I put this blogpost up there was because I felt there is an inaccuracy in Roger's post and the MSDN articles that Mika pointed us to. In short, they over-emphasize SPWeb.Dispose() which was true for WSS2, but for WSS3 it is unecessary.

I'll explain with an example.

Examine this code here -->

void SPListBreakRoleInheritanceLeak()


{


using (SPSite siteCollection = new SPSite("http://moss"))


{


using (SPWeb web = siteCollection.OpenWeb())


{


SPList list = web.Lists["ListName"];


list.BreakRoleInheritance(true);


// SPWeb list.ParentWeb leaked here, requires call to list.ParentWeb.Dispose()


} // SPWeb object outerWeb.Dispose() automatically called


} // SPSite object siteCollection.Dispose() automatically called


}

.. Now Roger says that ..


SPWeb list.ParentWeb leaked here, requires call to list.ParentWeb.Dispose()

.. That's not true. Calling SPSite.Dispose (which immediately ensues as the next using block "}"), will call ParentWeb.Dispose().

So in reality, both the MSDN articles, and Roger's blogpost have unecessary emphasis on calling SPWeb.Dispose() .. which IMO is unecessary.

Sahil


On 9/29/2008 9:33:25 PM Harish said ..
Hi Sahil,

Thanks for the tip, to the point and significant as usual. Never in my life would I have imagined Dispose as a pretty girl! Finding it hard to get the image out of my mind now :)


On 2/6/2009 8:05:57 AM Wouter said ..

On 12/2/2009 9:35:06 PM TPL said ..
I would know to that ..


Is it possible I get a value from Assigned To column and set it into another textbox?