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.
This entry was posted in SharePoint. Bookmark the permalink.

One Response to SharePoint’s disposing problems

  1. Tarique says:

    Hi,Really Nice Post!Regards,Tarique kamal

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s