Stick With What Got You Here

1. May 2010 03:44 by Todd Carter in Development, SharePoint  //  Tags:   //   Comments (1)

I think its safe to assume most SharePoint developers which have been working with the product for any length of time have seen code similar to the code below below. Its a common problem, disposable objects which are not disposed. There has been a ton written on this problem and Roger Lamb wrote a great tool called SPDisposeCheck which you run against your assemblies to determine if they may be failing to call Dispose.

public partial class VisualWebPart1UserControl : UserControl
{
  protected void Page_Load(object sender, EventArgs e)
  {
    SPSite site = new SPSite("http://intranet.contoso.com");
    foreach (SPWeb web in site.AllWebs)
    {
      Response.Write(web.Name);
    }
  }
}

Visual Studio 2010 includes a new Code Analysis rule which can check for these problems too. Now I am no SKU king so I cannot tell you what SKUs come with Code Analysis and which do not. I do know three things about this topic, (1) Not all Visual Studio 2008 SKUs included this feature, (2) I am guilty of being one of the many Microsoft minions which when we install Microsoft products look for versions with names like “Enterprise”, “Datacenter”, “Ultimate”, “Uber”, etc. (3) I am running Visual Studio 2010 Ultimate and it has Code Analysis built-in.

You can enable Code Analysis from the Project Properties dialog and choosing the “Code Analysis” tab at the bottom and then clicking on the “Enable Code Analysis…” checkbox which I have highlighted here.

code_analysis

For the code I have include above here is what Visual Studio Reports…

CodeAnalysis_Error

At this point a pain of disappointment should be felt; Visual Studio 2010 only found one of the two Dispose issues, that is a 50% success rate if you are that type of person; if you are the other that is a 50% fail rate – pick your side but it is unacceptable for such as small and trivial test.

Now how does SPDisposeCheck fair? SPDisposeCheck found both issues!

SPDisposeCheck_findings

Conclusion
Although Visual Studio 2010 has many great code analysis rules, and I use them frequently I personally would continue to use SPDisposeCheck as part of my development toolset.

Comments (1) -

Jeremy Thake
Jeremy Thake
5/1/2010 8:04:18 PM #

Todd you should check out my webcast on how to hook up SPDisposeCheck as a Static Code Analysis rule on @SPDevWiki Wink
www.sharepointdevwiki.com/.../SPWebCast+012+-+Introducing+SharePoint+2010+(SP2010)+Development+to+ALM+(VS2010+and+TFS2010)

Pingbacks and trackbacks (1)+

Comments are closed

Month List