in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Aaron Robertson-Hodders SharePoint Blog

Best Practice : Using disposable objects in SharePoint

I found this article on MSDN via Andrew's post posing the question "So is it best practice to only use C# for SharePoint development?". A good question and one that will have plenty of comment from all I imagine. For what it's worth (although not the point of this post), I'm a fan of C# because I like using (SPSite currentSite=new SPSite(http://Server)) ...

The interesting thing I took from the MSDN article was that objects that are accessed via SPSite objects, like RootWeb and ParentWeb need to be explicitly disposed of when you're finished with them. I had not appreciated this. I figured that if you 'used' using on the SPSite object then that would dispose all the members etc of that object. Not so say our MS friends! Smile

This means:

String str;
using(SPSite oSPSite = new SPSite("http://server"))
{
   str = oSPSite.RootWeb.Title;
   str = oSPSite.RootWeb.Url;

   ... additional processing on RootWeb ...

oSPSite.RootWeb.Dispose();
}

And, in the interest of fairness: 

Dim str as string
Dim oSPSite as SPSite

oSPSite=new SPSite(http://server)

str = oSPSite.RootWeb.Title
str = oSPSite.RootWeb.Url

... additional processing on RootWeb ...

oSPSite.RootWeb.Dispose()
oSPSite.Dispose()

Not really that difficult, but it will make a difference to your memory usage, have positive performance benefits and keep the "Potentially excessive number of SPRequest objects (11) currently unreleased on thread" error messages out of the already huge logs!

Regardless of you language of choice, this article is definitely worth a read if you are coding for SharePoint!

 

Comments

 

adrh said:

I just had a thought which I heard somewhere and the article on MSDN backs it up:

Objects obtained using SPContext, ie. SPContext.Current.Site DO NOT need to be disposed and shouldn't because it might cause issues to SharePoint internally.

This got me thinking, what about the object under these Context-obtained objects? The article talks about AllWebs and OpenWeb, which makes sense, and so I am assuming that RootWeb would need to be disposed, meaning this would be ok (and not cause instability?!):

SPSite currentContextSite = SPContext.Current.Site;

using (SPWeb currentRootWeb = currentContextSite.RootWeb)

{

  ...

}

November 13, 2007 7:31 PM
 

Alex Angas said:

Hi there,

I think it depends on your definition of explicitly disposing. The top of the MS article you linked to states 'You can automatically dispose of SharePoint objects that implement the IDisposable interface by using the Microsoft Visual C# using clause'. I've read another blog post (can't find it now of course) that says you must always use using because calling Dispose() isn't enough - although I don't think it explained why.

The good thing about using() is that it is supposed to wrap the code in try/finally to ensure the object is disposed even if an exception occurs (see forums.microsoft.com/.../ShowPost.aspx).

Speaking of those "Potentially excessive number of SPRequest objects..." errors by the way, have you ever tried setting the reg key it describes in the error message to see how much of MS' code doesn't appear to use dispose properly? For some reason I get it all the time when using foreach over SPListItem objects in List.Items, and I'm very diligent about disposing SP objects correctly.

Cheers, Alex.

November 14, 2007 8:22 AM
 

adrh said:

Alex,

I have seen information that suggests close() then dispose() too, and given it seems so contentious as to which is enough using seems like the plan!

I had wondered about the whole exception thing when using using ;-) and in my mind that is an excellent reason, rather than doing the whole try/catch/finally, check if null dispose() thing!

Yeah, I did turn on that resgistry flag at some point (although not in my current environment). And the current ones I see I'm convinced are related to something than I can't dispose myself! Meaning it probably is somewhere in the MS code.

It doesn't sit well though, because the stack trace obviously includes references to my code, even though I'm sure that it's not caused by my code directly! I think it is also related to list iteration as you found...

November 14, 2007 5:07 PM
 

Colin said:

Correct me if I'm wrong, but the problem with .RootWeb (and all the other SPWeb objects you need to dispose of) is that accessing the propery creates a new instance of an SPWeb.

That means that in your code sample, you're actually creating 6 new SPWebs each of the 6 times you use oSPSite.RootWeb. 5 of those webs aren't disposed of, and the last one was created just to be immediately disposed (oSPSite.RootWeb.Dispose()).

What you want to do instead is add the line:

     using( SPWeb rootWeb = oSPSite.RootWeb )

...at the beginning of your code and just use the rootWeb instance in the block that follows.

June 11, 2008 11:44 AM
 

adrh said:

Well Colin, I'm not going to correct you because I'm not actually sure if you're wrong (or right!). I would have thought that the first time you access the property that the object was created and thereafter it was just used.

Having said that, I would have thought that things would have been disposed of correctly in the first place, so who knows. ;-)

I you do know, I'd love to hear!

In reality, the code you posted is exactly what I do in practice with anything that I think needs disposing!

June 11, 2008 4:31 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

About adrh

I'm a Kiwi, living in Australia (for the past 10 years). I work for MacroView, a consulting firm in Sydney specialising in SharePoint solutions, products (WISDOM Document Management extensions, Custom Search solutions) and consulting around Enterprise Search. My background includes experience developing solutions in VB, VB.Net C#, ASP, ASP.Net, SharePoint (from V1) and Office.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts