Welcome to MSDN Blogs Sign in | Join | Help

Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

For the past two years or so, I've constantly worked with customers on issues related to not properly disposing of SPSite/SPWeb and other objects when necassary in long running applications, web parts etc.  We've pushed for SDK updates etc where necassary, but we didn't have a complete "Developers Guide" on this subject alone.

Scott Harris took the initiative and took all the hard work that I, Chris Gideon, himself and others have struggled with over the past two years and put it into a great document along with Mike Ammerlaan (A PM for the SharePoint Product Group)

After many weeks of composing, reviewing, editing, fact checking :) We've completed it and it is now available:

Best Practices: Using Disposable Windows SharePoint Services Objects

http://msdn2.microsoft.com/en-us/ms778813(office.12).aspx

Hope this helps, as it's part of my daily mantra with folks when I discuss this.  (Now I need to go an make sure I'm practicing what I preach in my tools :))

- Keith

Published Thursday, June 15, 2006 1:42 PM by krichie
Filed under: ,

Comments

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Nice! I always wondered when to dispose/close the objects and when not to ;)
The general rule was if the Context was used not to dispose/close that object and dispose/close all the others.

Friday, June 16, 2006 3:46 AM by Robin

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Great article!

I assume the same practises are applicable to V2 as well?
Friday, June 16, 2006 6:08 AM by Richard

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Robin, correct...The general rule was dispose unless it was derived from a context object.   As the article notes, we keep an internal list of those derived from the context and dispose of them internally.

Richard, actually this article "IS" for V2.  Once V3 is complete, I believe their will be some errata to this article for that.
Friday, June 16, 2006 7:10 AM by Keith Richie

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Great article! Thank you for sharing the best practices. If I have to nitpick, there seems to be a typo in the section regarding SPSite.AllWebs [] Index Operator. Shouldn't "oSPSite = oSPSite.AllWebs[i]" be "oSPWeb = oSPSite.AllWebs[i]" in the following code snippets?

Bad Coding Practice #1
Good Coding Practice #1
Good Coding Practice #3
Friday, June 16, 2006 8:47 AM by Hao Zhai

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Again, this may sound like nitpicking, but in the section regarding SPSiteCollection [] Index Operator, "oSPSite" should have been declared outside of the for loop as "SPSite oSPSite;" in the following code snippets:

Bad Coding Practice #1
Good Coding Practice #1

Don't worry; these minor typos did not prevent me from giving the article a perfect rating of 9. It truly is a great write-up of the subject and I've forwarded it to everyone on my development team.
Friday, June 16, 2006 9:01 AM by Hao Zhai

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Hao, correct on item one, it should be
oSPWeb = oSPWite.AllWebs[i].  I'll inform the content team.  

On the second one.  It would just create an additional managed reference for each iteration, rather than one reference (If it were defined outside of the loop).  Of course, each managed reference would be cleaned up, but I see your point of code tightening.

I for one would perform code tightening myself and place the object outside of the for loop, but either way, a new object instance is going to be created each time.
Friday, June 16, 2006 9:27 AM by Keith Richie

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Fix these last 2 typos and the article will be perfect!

In the section regarding SPWeb.Webs [] Index Operator, oSPWebe.Webs should be oSPWeb.Webs in Bad Coding Practice #2.

The if statement is missing a “)” for the code snippet right before “Conclusion”.
Friday, June 16, 2006 9:50 AM by Hao Zhai

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

On the second one, oSPSite was never declared (outside or inside the loop) before it was referenced. The compiler won't like it.
Friday, June 16, 2006 10:06 AM by Hao Zhai

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Ah, I gotcha now.  Yes, it was never declared.
Yep, I'll note that too.

Thanks Hao!!!
Friday, June 16, 2006 10:25 AM by Keith Richie

# re: Dispose of those Objects! SPSites, SPWebs, and SPBears oh my!

Great Article!  Combing through it now  :)
Friday, June 16, 2006 12:17 PM by mkruger

# SharePoint and objects disposal, the definitive guide!

One of those difficult things to handle in the construction of SharePoint related code is the cleaning...
Sunday, June 25, 2006 9:55 AM by Serge van den Oever [Macaw]

# Music » Keith Richie : Dispose of those Objects! SPSites SPWebs and SPBears oh my!

# Keith Richie Dispose of those Objects SPSites SPWebs and SPBears oh my | bird baths

New Comments to this post are disabled
 
Page view tracker