Question

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?

Was it helpful?

Solution

Yes, dispose the SPWeb here each time!

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top