Gunnar Kudrjavets

Paranoia is a virtue

Hunting down the missing calls to Dispose()

Today I had five different meetings. Yes, that’s how my average day looks like. Between these meetings there are slots of time I use to read and write e-mail and do something I call "real work" ;-) The interesting discovery for today was that I found a pattern in our code base where we create number of different objects implementing IDisposable interface and we accidentally forgot to call Dispose()  method on them. As almost anyone traveling in the managed world knows, this means that the objects won’t be properly cleaned up till garbage collection kicks in at some point which because of the nondeterministic nature of garbage collection may happen whenever CLR decides it’s necessary.

Why do I care ;-) Well, because I work on the server product and we really-really-really want everything cleaned up in timely fashion to keep the working set as low as possible. When working set keeps increasing or handle count keeps growing then we start getting pretty nervous around here.

The typical code pattern may look like this:

{
    ...

    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);

    //
    // Do something with 'sw', but never call 'Close()' when finished.
    //
}

Everyone knows the saying "Where There's Smoke There's Fire". For the practical applications I’ll borrow the quote from Glenford J. Myers’s "The Art of Software Testing" (page 15):

"The probability of the existence of more errors in a section of a program is proportional to the number of errors already found in that section."

So, if you’ll ask me what I’ll be doing tomorrow between meetings then the answer is that I’ll be checking the related assemblies for the similar patterns ;-)

Posted: Jun 30 2004, 08:29 PM by gunnarku | with 7 comment(s)
Filed under:

Comments

Uwe said:

Is that something that FxCop can check for?
# July 1, 2004 12:25 AM

Gunnar Kudrjavets [MSFT] said:

Probably yes, but I'm not FxCop advanced user yet to answer this question as somebody whose opinion can actually count ;-) While looking at FxCop documentation at http://www.gotdotnet.com/team/fxcop/gotdotnetstyle.aspx?url=DocFrameset.htm there are number of rules under “Rule Topics | UsageRules” which deal with IDisposable.Dispose, but not with this particular case.

Latest MSDN Magazine contained a nice article by John Robbins about FxCop (http://msdn.microsoft.com/msdnmag/issues/04/06/Bugslayer/). Maybe at one beautiful day I’ll have enough time to write my first rule using the new cool Introspection engine ;-)
# July 1, 2004 12:48 AM

David Levine said:

For our own classes when a Dispose method is exposed it is treated as part of the contract of the class, such that failing to call it is a violation of the API contract. We use a pattern where any class that implements a Dispose method must also implement a finalizer, and if the finalizer ever gets called we treat it as a programming error; in debug builds it can throw an exception, log it, etc. and in release builds it just calls the Dispose method - it's a policy decision that can be changed.

There are performance implications here that we are aware of, but we made a decision that correct coding was more important than squeezing every last bit of perf from the product; it is a contentious issue.

Its tough enough to get it right with our own classes, but trying to track down missing Close/Dispose calls on BCL classes feels like we are fighting the language rather then using it. It's even more difficult when trying to account for exception paths that can be taken, and still doing the right thing.

I wish C# supported a mixed programming model so that C++ style destructors could be supported sideXside with C# semantics. For that matter, I wish the C# compiler supported inline IL (similar to the old #pragma _asm syntax).

# July 1, 2004 8:15 AM

Sean Schade said:

You wouldn't believe how many developers believe it isn't neccessary to call dispose. I was just discussing Dispose on my blog last week. Look at some of the comments I received!

http://blog.magenic.com/seans/archive/2004/08/05/247.aspx
# August 10, 2004 9:00 AM

TrackBack said:

# August 10, 2004 12:14 PM

nodeFedEpoere said:

I added your blog to bookmarksa

# January 31, 2012 4:59 AM

bbieviur@gmail.com said:

To me they are all so neutral, they disappear in to the rest of her outfits.this post is extraordinary louis vuitton bags sale http://louisvuittonbagsforsale.webs.com%2

# April 21, 2013 5:17 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)