Monday, March 9, 2009

To Dispose or not to Dispose - that's the question

It's a good coding practice to tidy up after yourself to not waste system resources and there's good resources out there to help you learn the techniques. One of my favorites is http://code.msdn.microsoft.com/SPDisposeCheck

But recently I had a workflow that started dying on me and throwing up in the eventviewer with a
.NET Runtime version 2.0.50727.3068 - Fatal Execution Engine Error. The error assumed problems with the dotnet framework, but repairing the framework and applying hot fixes didn't solve the issue. And the strange thing was that it was only when adding new item it happened, where as the workflow ran perfectly when updating items. The major difference was that the item adding was triggered from an eventhandler and therefore ran under the System account, but item updating occurred when a user changed the document. I also discovered that part of the workflow ran, before it died, so I put in excessive logging and ended up at a SPWeb.Dispose line. Once I commented out these lines the error disappeared.

// Disposing here will cause workflow to fail
// web.Dispose();
I'm not a 100% sure this is the right answer, but running in System account seems to prohibit the use of Dispose. Anyway, I left these SPWeb objects to be taking care of by the garbage collector, maybe it was afraid of being unemployed...

No comments:

Post a Comment