Everything SharePoint and XAML

My continuous learning of SharePoint, XAML, Silverlight, Windows Phone 7, Windows 8, Office, VSTO, C#…

SharePoint Object Dispose Guidance

Posted by Steve Pietrek on February 2, 2009

Last week, the Microsoft SharePoint team released SPDisposeCheck. SPDisposeCheck is a tool which will check your code and alert you when SharePoint objects are not being disposed of properly. Keith Dahlby has a good post where he warns people that SPDisposeCheck is not 100% perfect and should not be the end all. After the SPDisposeCheck tool was released to the public last week, I ran it against my source code for a decent size project I finished up with a few weeks ago. Fortunately, there were no issues reported. I am not posting to gloat; rather list things I did and resources I used to improve my code to avoid leaks.

  1. Learn the SharePoint object model. I know, obvious but…
  2. Understand what Dispose is. Roger Lamb has an excellent post on the subject. Best way to avoid leaks is to understand why dispose is necessary. Roger lists the Dispose Patterns in the post.
  3. Read (better yet, memorize) the Best Practices: Using Disposable Windows SharePoint Services Objects article written by the Microsoft SharePoint team. It was recently updated last month (January 2009).
  4. Read blogs. There are highly intellectual folks who write brilliant posts on the disposable subject. Some posts I have linked to in the past have been written by Roger Lamb, Stefan Goßner, Waldek Mastykarz, and Reza Alirezaei. There are numerous others but this will get you jump started.
  5. Use Extension Methods and code libraries. The folks above have numerous examples of dispose code in their posts. They have researched it, written about it, and argued on it. Bundle it up and reuse it.
  6. Head over to Jeremy’s SharePoint Dev Wiki and read through this article.
  7. Become intimate with the SharePoint logs (ULS). Review the logs after running code which accesses the SharePoint Object Model and search for anything related to the SPRequest object. It will list the call stack so you can see what code is leaking. I was amazed how many issues I found by reading through the logs when I “thought” I had an handle on the proper usage of disposing SharePoint objects. Sure it is painful but well worth the effort.
  8. Be careful passing around SharePoint objects as parameters.
  9. Use the SPDisposeCheck tool to alert you of any leaks you may have missed.
Advertisements

Sorry, the comment form is closed at this time.

 
%d bloggers like this: