Stick With What Got You Here

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("");
    foreach (SPWeb web in site.AllWebs)

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.

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

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!

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.

One thought on “Stick With What Got You Here

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s