Saturday 31 January 2009

Disposing SharePoint Services Objects Updated (2009) and SharePoint Dispose Checker

Microsoft has finally released an update to the "Best Practices: Using Disposable Windows SharePoint Services Objects" article. If you've never heard about, glanced through, or read this article, it's required reading for every WSS/MOSS developer and worth printing off.

It's terribly important to remember that, even though you're writing code for a managed environment (.NET), you're not always dealing with managed objects; think of your MOSS objects (well, some of them anyway) as unmanaged resources that require special handling just like files and database connections. If you don't dispose of these objects properly, they'll sit in memory until the IIS application pool falls over and your site crashes. It happens and the existence of this article suggests it happens too much! The flip side is disposing of MOSS objects that shouldn't be disposed, which can also have negative consequences.

To coincide with this guidance, the SharePoint Dispose Checker tool (SPDisposeCheck) has also been released on the MSDN Code Gallery. Pointing this tool at your custom MOSS assemblies will tell you where you've gone wrong. The tool won't find every problem and may also report errors which are not actually problems but in general it's a great addition to your toolbox and a perfect compliment to the guidance. The tool can be used with MOSS or WSS assemblies. Note the -xml parameter doesn't work with this release--leave it off the call and pipe the output to a txt file. 

The tool is installed to "C:\Program Files\Microsoft\SharePoint Dispose Check" and running against the sample spits out a list of problems that look something like this:
ID: SPDisposeCheckID_160
Module: SPDisposeExamples.exe
Method: SPDisposeExamples.SPWebLeak.SPLimitedWebPartManagerLeak
Statement: webPartManager := page.{Microsoft.SharePoint.SPFile}GetLimitedWebPartManager(1)
Source: C:\ROGERLA\Demo\SPDisposeExamples\SPWebLeak.cs
Line: 42
Notes: Dispose/Close was not called on SPLimitedWebPartManager.Web
More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_160
Interpreting the SPDisposeCheck's output isn't difficult but note the tool may output a lot of information before telling you about the problems encountered. Scanning the westernaustralia.com .dlls resulted in a 10MB text file to report three problems so I'd suggest you start reading the output file from the bottom up or run the tool without the -debug parameter. Also make sure you read the readme file as it's not only quite useful but brief.

Finally, despite being labelled "Best Practice", this guidance has danced around since it was first published in 2006. But don't worry, Roger Lamb keeps and excellent, up-to-date
blog post on how to implement this guidance and any changes that come up. Apparently Roger's blog will be the default location where updates are published prior to incorporation into the doc proper. 


Custom-Built Microsoft Office SharePoint Server 2007 Branded Sites and Webpart Development - info@mediawole.com

2 comments:

  1. The tool works fine for WSS or MOSS - it's the SPDisposeExamples solution that has MOSS references.

    Cheers ~
    Keith

    ReplyDelete
  2. Fair point! I've corrected the post.

    ReplyDelete

Spam comments will be deleted

Note: only a member of this blog may post a comment.