Run SPDisposeCheck as Build Task and Automated Unit Test

SPDisposeCheck is a valuable tool devloped by a small team within Microsoft Premier Support led by Roger Lamb that inspects an assembly to ensure that SharePoint API objects are properly disposed. Failure to properly dispose SharePoint API objects can cause memory leaks. The SPDisposeCheck tool can be downloaded here:

 http://download.microsoft.com/download/B/4/D/B4D279A0-E159-40BF-A5E8-F49ABDBE95C7/SPDisposeCheck.msi

 Running SPDisposeCheck has always been a manual procedure that a SharePoint architect or team lead has to make sure is run against all new code before a release; or worse run afterward in order to find a leak that is already in production. A preferable approach would be to add this to a unit test that can be part of a restrictive check-in policy in TFS to ensure no code, in which SharePoint API objects are not properly disposed, can be checked in. Here is how that can be accomplished:

 Copy the SharePoint Dispose Check folder as a sub folder of the solution folder as demonstrated below:

 

 
 Find all projects under the solution that reference the Microsoft.SharePoint namespace and select properties on the project file. Select the Build Events tab and enter the commands below into the Post-build events text box for On Successful Build:

 cd $(ProjectDir)

“..\SharePoint Dispose Check\SPDisposeCheck” “$(TargetPath)” > “$(ProjectName)”.SPDisposeCheck.log

findstr /C:”Total Found:” “$(ProjectName)”.SPDisposeCheck.log

 The Post-build events above will run the SPDisposeCheck tool against the compiled assembly outputting to a file that consists of the assembly name appended with .SPDisposeCheck.log. When the project is compiled the developer will see in the output window “Total Found: X” where X is the count of SPDisposeCheck errors in the assembly. See below:

 

 Any value other than “Total Found: 0″ means that a SPDisposeCheck error has been spotted. This is a good start, as developers can be instructed to look for this value but not good enough because it’s too easy to ignore. The next step would be to add this to a unit test which can be tied to a TFS restricted check in policy which cannot be ignored. To do this open the class file right click the class and select “Create Unit Tests”

 

 Add a new unit test to the automatically generated unit tests as seen below: The text specified in the File.OpenText section should be replaced with the name of the assembly in question.


///
///A test to ensure SharePoint objects are properly disposed
///
[TestMethod()]
public void SPDisposeCheckTest()
{
     StreamReader streamReader = File.OpenText(@"..\..\..\Intellinet.SharePoint.Utility\Intellinet.SharePoint.Utility.SPDisposeCheck.log");
     String logFile = streamReader.ReadToEnd();
     Assert.IsTrue(logFile.Contains("Total Found: 0"), logFile);
}

This unit test will fail if the file generated by SPDisposeCheck has any value other than “Total Found: 0″ and the contents of the file including the links to Roger’s examples of correcting the particular type of dispose defect can be viewed by the developer by clicking the failed unit test.


Since SPDisposeCheck is performing static analysis on the IL a better approach would be to reverse the SPDisposeCheck code and create a Static Analysis rule that would be available as one of the Static Analysis rulesets in TFS. Until that day this can be a very useful approach for SharePoint architects to control code quality and prevent SharePoint API dispose memory leaks.

About these ads

9 Responses to “Run SPDisposeCheck as Build Task and Automated Unit Test”

  1. Tweets that mention Run SPDisposeCheck as Build Task and Automated Unit Test « Stephen Vick's Blog -- Topsy.com Says:

    [...] This post was mentioned on Twitter by John Ferringer, Matt Ranlett. Matt Ranlett said: Hey you #SharePoint people – do you use SPDisposeCheck to prevent memory leaks? Do it automatically! http://bit.ly/7smO1I [...]

  2. uberVU - social comments Says:

    Social comments and analytics for this post…

    This post was mentioned on Twitter by mranlett: Hey you #SharePoint people – do you use SPDisposeCheck to prevent memory leaks? Do it automatically! http://bit.ly/7smO1I

  3. Tip Tricks Says:

    Hey, ok, I get it, I guess – but does this really work?

  4. Jeremy Thake Says:

    I took your idea and built a custom msbuild task so that it hooks up into automated builds. Thanks again for the inspiration…just wish they’d written the tool as an API or that the XML output worked properly so could give more feedback for build!

    Webcast is here:
    http://www.sharepointdevwiki.com/display/SPPodCasts/2010/01/24/SPWebCast+009+-+SharePoint+2007+Development+with+Continuous+Integration+%2841+mins%29

  5. SPDisposeCheck is your friend « SurePoint Says:

    [...] great idea would be to configure SPDisposeCheck to automatically run as part of your Visual Studio build process and/or prior to being checked in to a code [...]

  6. NgocAnh Says:

    “To do this open the class file right click the class and select Create Unit Tests” => It is error : The following error was encountered white reading module ‘Microsoft.SharePoint': Assembly reference not resolved: Microsoft.SharePoint.Library, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce11e9429c.
    Pls. help me to create Unit Test Automated
    Thanks.

  7. deyaert Says:

    You don’t need to write a unit test, if you add the SPDisposeCheck.exe inside build configuration. When it finds memory leaks it exits with a different exit code (normally breaking the build process)

  8. Run SPDisposeCheck as an Automated Unit Test on your Build Server « Mel's space Says:

    [...] runs on your Build Server every time code is checked in. To that end, I came across a very helpful article, which creates just such a unit [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: