This is a must have! If you do any kind of SharePoint development then you should strongly consider downloading the recently released SPDisposeCheck tool: http://blogs.msdn.com/pandrew/archive/2009/01/29/spdisposecheck-v1-3-1-is-released.aspx. This tool analyzes your assemblies and helps to identify potential memory leaks. I should note that you need to truly evaluate each result because not every item should be considered a show stopper – you need to understand the various dispose patterns and should use this tool as a quick way to check for things that your (or your peers) might have missed.
And, in case anyone is wondering – yes, I have run this tool against my extensions and here are the results:
----------------------------------------------------------
Total Found: 0
----------------------------------------------------------
Modules Checked: 1
----------------------------------------------------------
Lapointe.SharePoint.STSADM.Commands.dll
----------------------------------------------------------
Modules Ignored: 0
----------------------------------------------------------
----------------------------------------------------------
Methods Ignored: 0
----------------------------------------------------------
Sometimes I know what I’m doing 🙂
Running it against my PowerShell cmdlets results in 6 issues found but they are all false positives due to the fact that I’m returning an SPSite
and SPWeb
object via a base property/method and I’m explicitly expecting the caller to dispose of these objects. Here’s an example of that output:
ID: SPDisposeCheckID_110
Module: Lapointe.SharePoint.PowerShell.Commands.dll
Method: Lapointe.SharePoint.PowerShell.Commands.Proxies.SPSiteInfo.GetSPObject
Statement: CS$1$0000 := new Microsoft.SharePoint.SPSite(this.{Lapointe.SharePoint.PowerShell.Commands.Proxies.SPSiteInfo} get_ID())
Source: W:\work\Lapointe.SharePoint.PowerShell.Commands\Lapointe.SharePoint.PowerShell.Commands\Proxies\SPSiteInfo.cs
Line: 98
Notes: NOTE: This instance was returned from the method and can likely be ignored as long as the type is disposed in the caller.
SPDisposeCheck will automatically determine if the instance was disposed in the calling method
Disposable type not disposed: Microsoft.SharePoint.SPSite
***This may be a false positive depending on how the type was created or if it is disposed outside the current scope
More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110
If you have similar false positives in your code you can have the tool ignore these by adding the SPDisposeCheckIgnore
attribute to your method or property. Here’s a snippet from the documentation included with the tool:
Accepting an Issue
If you have investigated a reported issue and are satisfied that it doesn’t represent a problem you can have it ignored by the tool. To do this add the declaration of the SPDisposeCheckIgnore attribute to the method where the error is shown and specify the error to ignore. You also need to add the declaration for SPDisposeCheckIgnore to your project. You can safely change the namespace of SPDisposeCheckIgnore to match your project to avoid additional using statements in your source files. Here is an example, which you can see in the SPDisposeExamples project.
[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_110, "Don't want to do it")]