views:

325

answers:

3

Is there a tool that can scan your code and determine which objects that implement IDisposable are not being disposed in a code base at compile time or runtime?

I have possible areas in the code that are not disposing objects but it's hard to look back and see which objects require this in the first place.

+2  A: 

Both CodeRush/Refactor Pro and Resharper will, at code time, in Visual Studio, show you undisposed objects.

Scott Weinstein
+1, but how reliable is that?
Henk Holterman
I'll consider this the answer after I try Resharper and these other tools. I'd like to keep this question opened to get more ideas as well but I appreciate the answer. +1 for you.
Jeremy Edwards
A: 

The destructor of a class is called when the garbage collector finally cleans up classes that are no longer in use. What you could do is put a check in the destructor that "Dispose" has been called. It is advised against actually using the destructor, but for testing purposes, it can be useful.

The only problem with this method is that you will have to create your own class that inherits from the one you need to test for IDispose and override the Dispose method (so you can set a flag saying that it has been called), initialize the flag in a constructor and check it in the destructor (which you implement with "~ClassName() { ... }")

Grant Peters
Could somebody comment on why this answer was downvoted? Is it unreliable?
Eric
+1  A: 

The ANTS memory profiler from red-gate will help with this at runtime. It's one of my favorite tools.

Eric