views:

468

answers:

4

Hi, i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable objects declared in. I mean something like resharper or another visual studio plugin.

thanks.

+7  A: 

I know what you mean. I don't know, but look at FxCop. It might have a rule in there somewhere that checks whether objects implementing IDisposable are not disposed. Just a hunch, mind.

UPDATE: Mitch Wheat writes:

FxCop includes the rule, thats says all types that derive from types that implement IDisposable should implement the Dispose() pattern

Thanks, Mitch.

Neil Barnwell
I was going to suggest FxCop, but couldn't find a suitable rule. Depending on how much code there is, however, you probably could write your own rule.
Michael Meadows
FxCop includes the rule, thats says all types that derive from types that implement IDisposable should implement the Dispose() pattern
Mitch Wheat
Thanks, Mitch. I've updated the answer to bring it to the OP's attention.
Neil Barnwell
A: 

Also, depending on whether you use systems like that, if you're using an IoC container, it might go through several layers of code before the service is returned to you through an interface, and it might not be trivial to handle IDisposable in such a case.

Perhaps the interface you resolved doesn't inherit from IDisposable, but the actual service class being used does? How to handle that? etc.

Lasse V. Karlsen
+2  A: 

You could do this with ReSharper. With ReSharper you can navigate implementations of any interface with ease by using Alt-End, but for a popular interface such as IDisposable this is not practical.

Here's what you could do:

  1. Go to Object Browser (Ctrl-Alt-J or View->Object Browser)
  2. Find System.IDisposable
  3. Right click and select "Find Usages Advanced" (ReSharper's menu item)
  4. User Find, check "Implementations", under Scope choose Solution
  5. You will get a list of all types (of your solution) implementing IDisposable. Those in bold are the ones you want - they implement IDisposable directly.

Hope that helps.

hmemcpy
+3  A: 

Usage Rules CA2213 (DisposableFieldsShouldBeDisposed) and CA2215 (DisposeMethodsShouldCallBaseClassDispose) within FxCop will catch where dispose isn't called correctly in your own classes but i don't believe there is anything out there to check dispose is always called though ironically there is a rule (CA2202) for DoNotDisposeObjectsMultipleTimes

Wolfwyrd