Does anybody know of a way to automatically find any variable, where the type implements IDisposable but the using construct is not used?
ie. a way to check for potentially unreleased unmanaged resources?
Also, is it possible to see the number and types of resource held by a running application?
There’s a code analysis rule for this:
http://msdn.microsoft.com/en-us/library/ms182289%28VS.100%29.aspx
This can be run from VS 2010 Premium or Ultimate or separately with FxCop:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=917023f6-d5b7-41bb-bbc0-411a7d66cf3c
Another thing I’ve seen done is to capture a stack trace when an
IDisposableobject is constructed and then if the finalize is hit (meaningDispose()was not called) log an error with the constructed stack trace. This is expensive so you may only want to do it in development, or only start collecting stack traces the second time your app runs into this problem (if you run into it once, you’re most likely going to run into it many times within a single app execution). This method works forIDisposableinstances that are longer lived (not just local variables). Of course it also only works for customIDisposableobjects since it requires custom code in the constructor/dispose/finalizer.