6

With all the focus on disposing all disposable objects in SharePoint, I wondered if there is any harm in disposing an object twice. Especially since SharePoint seems to do some disposing itself too. You have to check if SharePoint will do your disposing, or you have to do it yourself.

I know you can wreck havoc by disposing the wrong object (e.g. SPContext.Current.Site).

  • Yes, I know I answered my question directly after asking it, but it bugged my some time already and I couldn't find an answer here. Since all the focus is on calling dispose at the right places, I started wondering what would happen if you call dispose twice (or call it, when SharePoint will also dispose your object). – Dribbel Sep 23 '11 at 13:17
3

After some research I found the answer on the general description page of the IDisposable-interface.

If an object's Dispose method is called more than once, the object must ignore all calls after the first one.

So, if you accidently dispose an SharePoint-object twice, no harm done.

2

I highly recommend that you download and install the SPDispose Check tool. http://archive.msdn.microsoft.com/SPDisposeCheck

You have the option of including this as part of the build process inside of Visual Studio. I've found that it is helpful in finding things that were missed, but also it will point out when it does NOT need to be disposed. The errors also point to a specific error/note that you can then reference and learn the specific info for that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.