14

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.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
David Espart
  • 11,520
  • 7
  • 36
  • 50

4 Answers4

11

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.

Community
  • 1
  • 1
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
  • 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 Feb 26 '09 at 15:34
  • 1
    FxCop includes the rule, thats says all types that derive from types that implement IDisposable should implement the Dispose() pattern – Mitch Wheat Feb 26 '09 at 15:38
  • Thanks, Mitch. I've updated the answer to bring it to the OP's attention. – Neil Barnwell Feb 26 '09 at 15:41
10

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.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
5

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
  • 15,716
  • 5
  • 47
  • 67
0

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
  • 380,855
  • 102
  • 628
  • 825