SharePoint’s disposing problems

After returning from the MVP Summit I joined Microsoft Consulting Services as part of the Green Badge program (vendor).
On my first "real" project here, I had my first experience with Windows SharePoint Services 3.0.
I’m in charge of reviewing part of the code written by a customer’s developers for best practices in general and performance in particular.
 
One thing that you should be carefull with is disposing the objects which implement IDisposable.
That is specially important with a couple of SharePoint’s classes since they reference unmanaged COM components.
The problem is that (in my opinion) the SharePoint developers didn’t do a very good job of implementing the Disposable pattern.
 
There are tons of links out there pointing to problems and workarounds for them. Here’s a sample:
 
What makes the case even worse is the fact that:
  • Depending on how you got a reference to an object you should dispose it or not. SPSite should be disposed if you constructed it (new SPSite()). But you should NOT dispose it if you got it from SPControl.GetContextSite().
  • Several classes don’t dispose of there containing Disposable objects. SPSite.RootWeb for instance.
I’ll eventually blog more about these issues if I get allocated in more SharePoint projects.

One thought on “SharePoint’s disposing problems”

Leave a Reply