Disposing of SharePoint objects

Correctly disposing of SharePoint objects like SPWeb and SPSite have been discussed and documented several times, but I think developers constantly needs to be reminded of the importance of proper use of dispose(). Yesterday Stefan wrote an interesting article you should check out if you don’t catch the error in this code:

public void enumSiteCollection(SPSiteCollection spSites, int min, int max)

{

for (int i = min; i<max; i++)

{

    if (spSites[i] != null)

   {
            DoSomething(spSites[i]);

           spSites[i].Dispose();

    }

}

}

It really looks OK doesn’t it? Sorry to say it isn’t. Go to Stefan’s blog and read why.

 

If you haven’t yet downloaded the newly released tool SharePoint Dispose Checker – you really should do it right away. I have used it for quite some time and it is really a time saver for finding potential dispose errors, especially if you are doing code reviews.