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

We have the following code:

    foreach (SPWeb website in collectionOfWebSites)
    {
           // Some code, no dispose of website

    }

SPDisposeChecker is reporting it as a memory leak. Should we dispose the website at the end of the foreach?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Yes, dispose the SPWeb here each time!

share|improve this answer
add comment

Someone might need to back up me, but if you're getting an object from that collection, it does actually create an SPWeb instance each time (the collection is just a collection of object references rather than the objects themselves?), so you will indeed need to dispose of the object after each iteration.

share|improve this answer
    
That is what I though, but after I posted the question I found this link blogs.msdn.com/b/rogerla/archive/2008/02/12/… which says that you need to dispose each instance of SPWeb. –  Shiraz Bhaiji Sep 8 '10 at 11:19
1  
I'm pretty sure as James says - it will create an SPWeb instance for each iteration. So yes you will need to dispose of it manually after you have finished with it. Although the SPWeb won't be accessible outside the foreach it will still waste resources if not disposed. –  Anonymous Sep 8 '10 at 11:19
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.