Share via


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

https://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

  • Anonymous
    June 15, 2006
    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.

  • Anonymous
    June 15, 2006
    Great article!

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

  • Anonymous
    June 15, 2006
    The comment has been removed

  • Anonymous
    June 16, 2006
    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

  • Anonymous
    June 16, 2006
    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.

  • Anonymous
    June 16, 2006
    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.

  • Anonymous
    June 16, 2006
    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”.

  • Anonymous
    June 16, 2006
    On the second one, oSPSite was never declared (outside or inside the loop) before it was referenced. The compiler won't like it.

  • Anonymous
    June 16, 2006
    Ah, I gotcha now.  Yes, it was never declared.
    Yep, I'll note that too.

    Thanks Hao!!!

  • Anonymous
    June 16, 2006
    Great Article!  Combing through it now  :)

  • Anonymous
    June 25, 2006
    One of those difficult things to handle in the construction of SharePoint related code is the cleaning...

  • Anonymous
    May 27, 2008
    PingBack from http://music.findsongbylyricsblog.info/keith-richie-dispose-of-those-objects-spsites-spwebs-and-spbears-oh-my/

  • Anonymous
    June 18, 2009
    PingBack from http://cutebirdbaths.info/story.php?id=3462