Checking Improper DISPOSE in assemblies using SharePoint API

In this article I will show how to check improper disposing of instance of SharePoint classes in an assembly.


Objective 
 
In this article, I will show how to check improper disposing of instance of SharePoint classes in an assembly. 

SharePoint Dispose Check tool 
 
This tool is available on CODEPLEX. Download this from HERE and install this. 
 
As of documentation of SharePoint Dispose Check tool on CODEPLEX; 
 
SPDisposeCheck is a tool to help you to check your assemblies that use the SharePoint API so that you can build better code. It provides assistance in correctly disposing of certain SharePoint objects to help you follow published best practice. This tool may not show all memory leaks in your code. Further investigation is advised if you continue to experience issues.
 
After successful installation of the tool finds some time to have a look on documentation. You can find documentation in your Start ->All Programs
 
How to use SPDisposeCheck?
  1. Open command prompt 

     
  2. Change directory to 

    1.gif
     
  3. Type  SPDisposeCheck.exe ; it will show the options available 

    2.gif
     
  4. If you want to redirect the output of an assembly check to a text file then just need to append standard redirect DOS operator as below 

    C:\\Program Files\..\..> SPDisposeCheck.exe assemblydll  >> somefilename.text

    3.gif
Let us say, you have a code in class library something like below 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace TestingDisposeinSharePoint
{
    public class Class1
    {
        public void  SnapOfContext()
        {
            SPSite site = new SPSite("http://c849uss:9722/Sites/Test1");
            SPWeb web = site.OpenWeb();
        }
    }
}
 
In above code DISPOSE is not managed properly.  So when we run DLL of above class library using SPDisposeCheck we will get below output.  Name of the DLL we are testing is TestingDispeseinSharePoint.dll. This DLL is inside folder c:\disposecheckingdll 
 
C:\Program Files\Microsoft\SharePoint Dispose Check>SPDisposeCheck.exe c:\disposecheckingdll\TestingDisposeinSharePoint.dll
 
Output
----------------------------------------------------------
C:\Program Files\Microsoft\SharePoint Dispose Check>SPDisposeCheck.exe c:\dispos
echeckingdll\TestingDisposeinSharePoint.dll
Note: This tool may report errors which are not actually memory leaks, otherwise
 known as false positives.
Further investigation should be done to identify and correct real errors.
It is designed to assist developers in making sure their code adheres to best pr
actices for memory allocation when using SharePoint APIs.
Please see the following for more information:
http://blogs.msdn.com/rogerla/
http://msdn2.microsoft.com/en-us/library/aa973248.aspx
http://msdn2.microsoft.com/en-us/library/bb687949.aspx
----------------------------------------------------------
 
ID: SPDisposeCheckID_110
Module: TestingDisposeinSharePoint.dll
Method: TestingDisposeinSharePoint.Class1.SnapOfContext
Statement: local0 := new Microsoft.SharePoint.SPSite("http://c849uss:9722/Sites/
Test1")
Notes:   Disposable type not disposed: Microsoft.SharePoint.SPSite
         ***This may be a false positive depending on how the type was created o
r if it is disposed outside the current scope
More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-20
07-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110
----------------------------------------------------------
 
ID: SPDisposeCheckID_120
Module: TestingDisposeinSharePoint.dll
Method: TestingDisposeinSharePoint.Class1.SnapOfContext
Statement: local1 := local0.{Microsoft.SharePoint.SPSite}OpenWeb()
Notes:   Disposable type not disposed: Microsoft.SharePoint.SPWeb
         ***This may be a false positive depending on how the type was created o
r if it is disposed outside the current scope
More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-20
07-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_120
----------------------------------------------------------
 
Total Found: 2
 
----------------------------------------------------------
 
Modules Checked: 1
----------------------------------------------------------
TestingDisposeinSharePoint.dll
----------------------------------------------------------
 
Modules Ignored: 0
----------------------------------------------------------
----------------------------------------------------------
 
Methods Ignored: 0

 
C:\Program Files\Microsoft\SharePoint Dispose Check>^A
 
So, from output we can clearly see that SPSite and SPWeb instance is not properly disposed.   Now let us modify the above code as below 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace TestingDisposeinSharePoint
{
    public class Class1
    {
        public void  SnapOfContext()
        {
            SPSite site = new SPSite("http://c849uss:9722/Sites/Test1");
            SPWeb web = site.OpenWeb();
            if (site != null)
                site.Dispose();
            if (web != null)
                web.Dispose();
        }
    }
}
 
In above code we are properly handling the dispose of SharePoint instances.  So on testing of updated DLL with 
 
C:\Program Files\Microsoft\SharePoint Dispose Check>SPDisposeCheck.exe c:\disposecheckingdll\TestingDisposeinSharePoint.dll
 
Output 
----------------------------------------------------------
C:\Program Files\Microsoft\SharePoint Dispose Check>SPDisposeCheck.exe c:\dispos
echeckingdll\TestingDisposeinSharePoint.dll
Note: This tool may report errors which are not actually memory leaks, otherwise
 known as false positives.
Further investigation should be done to identify and correct real errors.
It is designed to assist developers in making sure their code adheres to best pr
actices for memory allocation when using SharePoint APIs.
Please see the following for more information:
http://blogs.msdn.com/rogerla/
http://msdn2.microsoft.com/en-us/library/aa973248.aspx
http://msdn2.microsoft.com/en-us/library/bb687949.aspx
----------------------------------------------------------
 
Total Found: 0
 
----------------------------------------------------------
 
Modules Checked: 1
----------------------------------------------------------
TestingDisposeinSharePoint.dll
----------------------------------------------------------
 
Modules Ignored: 0
----------------------------------------------------------
----------------------------------------------------------
 
Methods Ignored: 0

 
C:\Program Files\Microsoft\SharePoint Dispose Check>
 
There is no error reported because all the dispose was properly managed. 
 
Conclusion 
 
In this article we saw how to use third party tool to check improper dispose on SharePoint API. Thanks for reading .