Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

SharePoint Dispose Checker Tool

5.00/5 (6 votes)
26 Jul 2012CPOL3 min read 35.9K   708  
The Dispose Checker Tool is a useful tool in finding non-disposed objects inside SharePoint projects.

Introduction

In this article we can get awareness over the Dispose Checker Tool which is a useful tool in finding non-disposed objects inside SharePoint projects.

What is the Dispose Checker Tool?

Dispose Checker Tool checks your project for unreleased SharePoint objects and reports them as errors. You can integrate this tool with Visual Studio 2010 and there is automatic error reporting on the build event.

Image 1

Following are the objects that are checked for disposal:

  • SPSite
  • SPWeb

Why we need this tool?

You might be thinking that why we need this tool although we have .Net Garbage Collector in place. Yes! It is true that there is .Net automatic garbage collection.  But there is a risk involved of unreleased objects occupying too much memory. This may lead to application pool restart occasionally creating delays during responses.

For example the following code induces non-disposed object:

Image 2

Plus there are unmanaged memory occupied by the managed server objects resulting in very high memory consumption. To be sleek with memory it is then advised to release memory.

So the best way of using SharePoint objects is to release the objects after code exit. The using statement is the recommended solution.  The other solution is to manually call the Dispose() method.

Where to download the tool?

You can download the tool from CodePlex and as there are many versions choose the right version appropriate for your SharePoint binaries.

Please note that sometimes the latest version may not work well with your binaries. The following link contains a valid download tested with SharePoint 2010 and Visual Studio 2010: http://archive.msdn.microsoft.com/SPDisposeCheck/.

How to use the tool?

Install the setup from downloaded from the above link and please remember to check the following options:

Image 3

On selecting the above options you will get the convenience of:

  • Using it from Tools menu as a Visual Studio add-in
  • Automatic execution of tool on each build

Testing the tool

You can create a new SharePoint 2010 Console Application. Add the following code inside the main method.

C#
static void Main(string[] args)
{
    SPSite site = new SPSite("http://localhost");
    SPWeb web = site.OpenWeb();
}

Build the project to continue.

Accessing the tool

You can access the tool from Tools menu of Visual Studio.

Image 4

On accessing the tool it will show the current project as Dispose Check enabled.

Image 5

Click on the Save and Analyze button to start analyzing your project. You should be getting the following results in the Error List.

Image 6

The error denotes the need for disposing object and you can see there is an MSDN link to learn more about disposing objects.

Solving the error

You can use the Dispose method of each objects or use the using statement to get rid of errors.

Image 7

Try building the project and you should see that the errors from Error List disappeared. (If automatic dispose check was selected while installation.)

You can practice this by opening your existing project and running the tool. If there are dispose check errors you should correct them.

Note: The whole idea behind using this tool is to make the code efficient by disposing unwanted objects after scope.  The examples over MSDN are having using clause to support the dispose pattern. You can refer the associated MSDN link for more information.

References

Summary

In this article we have explored the Dispose Checker Tool for SharePoint 2010. Following are the points to be summarized about Dispose Checker Tool.

  • Helps in finding non-disposed objects
  • Integrates well with Visual Studio 2010
  • The tool is free

The attached source code contains valid and invalid scenarios. You can check the errors running the Dispose Checker tool after building.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)