The Sharepoint team released a handy tool that helps you detect incorrectly (read: not) disposed objects. It provides assistance in correctly disposing of certain SharePoint objects to help you follow the published best practice.
Best practice is to dispose you objects that inherit from the IDisposable interface. There is more info about disposing objects in SharePoint on the msdn site. Knowing when to dispose ( or when not to dispose) can be tricky sometimes.
So when to Dispose? Shahil has 3 great rules to live by
1) Do not dispose objects you didn’t create. In other words, in a feature receiver, if “Parent” is SPWeb or SPSite – don’t be callin’ dispose on that. You didn’t create that object, and after your feature receiver is done, parts of the SharePoint framework may still need that object. This rule applies to general .NET.
2) Dispose is like a pretty girl, if you see it, call it… but don’t break rule #1. i.e. don’t call a pretty girl that isn’t confirmed unattached, .. if her large mammalian boyfriend finds out, he may knock your teeth out. This rule applies to general .NET as well.
3) If you call SPSite.Dispose, it will automatically call Dispose on all the contained SPWebs, thus unless you have an extremely borderline crazy logic that creates 200 SPWebs that you must dispose manually out of band, you should simply call SPSite.Dispose. This wasn’t the case in WSS 2 mind you!
On a side note: I recommend using “using” statements to dispose of objects (it is the easiest), but try catch blocks work also.
You can download the SPDisposeCheck tool here : http://code.msdn.microsoft.com/SPDisposeCheck




