Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am doing a whole bunch of things with one SPWeb and many SPLists. If I just dispose web in the finally section would that dispose all of SPLists that I instantiated? (btw, I understand the concept of using, so should I implement using instead?)

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

SPList objects cannot be disposed.

share|improve this answer
    
Thanks Wictor. I guess then run the spweb under the using umbrella should do it. Thanks –  Monica Contera May 23 '11 at 16:58
    
You should not always dispose SPWeb, it depends on the context of where you run the code. If you get your web's from SPContext you must not dispose these webs. As a rule of thumb, webs you create ourself, eg with sitecol.OpenWeb(), you should dispose. –  Anders Rask May 23 '11 at 19:17
add comment

I recommend installing and running this tool to check whether you are disposing your objects correctly: SPDisposeCheck

share|improve this answer
3  
SPDisposeCheck is a good start, but be aware of false positives. –  Anders Rask May 23 '11 at 19:19
    
Note that you can also tag up your project to ignore some of those false positives, when you've considered them and decided that that's what they are. See zimmergren.net/archive/2011/01/31/… –  Andy Burns May 24 '11 at 11:21
add comment

The rule is complicated, but in a nutshell, you should dispose of any object that you create yourself that implements the IDisposable interface.

In the context of SharePoint, this includes:

  • Any SPSite object you create using siteColl.OpenWeb(), returned by SPSiteCollection.Add, by the SPSiteCollection[] index operator, or yielded in a SPSiteCollection foreach loop
  • Any SPWeb object returned from SPSite.AllWebs.Add, SPWebCollection.Add, the SPSite.AllWebs[] index operator or yielded in an SPSite.AllWebs foreach loop.

There are many more cases outlined in the Best Practices: Using Disposable Windows SharePoint Service Objects article on TechNet.

Note that SPList is not in this list (because it does not implement IDisposable, nor is SPList.RootWeb (because you don't create the RootWeb object, you just get a reference to it).

Note that your code is unlikely to compile if you try to Dispose an object like SPList that doesn't implement IDisposable, because that object will not have a Dispose method. On the other hand, you won't get an error if you try to dispose RootWeb, because it is an SPWeb object with a Dispose method - but doing this will cause problems if you try to refer to that web or any of it's children later in your code.

The using statement is a bit of syntactic sugar, but I prefer to use it because it ensures that the object is always disposed correctly, and it doesn't require explicit code to ensure that this happens.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.