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

Comments (13)

  1. Robin says:

    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.

  2. Richard says:

    Great article!

    I assume the same practises are applicable to V2 as well?

  3. Keith Richie says:

    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.

  4. Hao Zhai says:

    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

  5. Hao Zhai says:

    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.

  6. Keith Richie says:

    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.

  7. Hao Zhai says:

    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”.

  8. Hao Zhai says:

    On the second one, oSPSite was never declared (outside or inside the loop) before it was referenced. The compiler won’t like it.

  9. Keith Richie says:

    Ah, I gotcha now.  Yes, it was never declared.

    Yep, I’ll note that too.

    Thanks Hao!!!

  10. Great Article!  Combing through it now  🙂

  11. One of those difficult things to handle in the construction of SharePoint related code is the cleaning…

Skip to main content