views:

173

answers:

5

I am working on a large project, and one of my tasks is to remove possible memory leaks. In my code, I have noticed several IDisposable items not being disposed of, and have fixed that. However, that leads me to a more basic question, how do I find all classes used in my project that implement IDisposable? (Not custom-created classes but standard Library classes that have been used).
I have already found one less-than-obvious class that implements IDisposable ( DataTable implements MarshalByValueComponent, which inherits IDisposable). Right now, I am manually checking any suspected classes by using MSDN, but isn't there some way through which I can automate this process?

A: 

I think something like the code below might work. It would have to be adjusted to load the correct assembly if run from an external tool though.

Assembly asm = Assembly.GetExecutingAssembly();
foreach (Type type in asm.GetTypes())
{
 if(type.GetInterface(typeof(IDisposable).FullName)  != null)
 {
  // Store the list somewhere
 }
}
ho1
This is not exactly what I was looking for. This code loops over the types <i>defined</i> in the current assembly, while I want to run it over the types being <i>used</i> in the current assembly. <br/>I can easily check if one of the classes defined in my project inplements IDisposable, however i want to check whether the standard library classes that I am using implements IDisposable or not.
apoorv020
@apoorv020: Should be able to run the above code on the .Net framework assemblies (any that are referenced by your project) as well I'd think, and then you could save a list of everything that implements `IDisposable` and then just do a search through your sourcecode for anything in that list?
ho1
@ho1 : The code by "Thomas Levesque" does exactly that. Thanks for your answer though, it pointed me in the right direction.
apoorv020
A: 

try this LINQ query:

var allIdisposibles = from Type t in Assembly.GetExecutingAssembly().GetTypes()
                      where t.GetInterface(typeof(IDisposable).FullName) != null && t.IsClass
                      select t;
this. __curious_geek
Don't think that would work, comment from MSDN: *The IsSubclassOf method cannot be used to determine whether an interface derives from another interface, or whether a class implements an interface*
ho1
thanks for correction.
this. __curious_geek
+4  A: 

Test your project with FxCop. It can catch all places where IDisposable objects are not disposed. You may need to do some work to disable all irrelevant FxCop rules, leaving only IDisposable-related rules.

For example, this is one of FxCop IDisposable rules: http://msdn.microsoft.com/en-us/library/ms182289%28VS.100%29.aspx

Note: you need to find both your own, .NET and third-party IDisposable objects which are not handled correctly.

Alex Farber
+2  A: 

Reflector can show you which classes implement IDisposable : just locate the IDisposable interface in Reflector, and expand the "Derived types" node

Another option, from code, is to scan all loaded assemblies for types implementing IDisposable :

var disposableTypes =
    from a in AppDomain.CurrentDomain.GetAssemblies()
    from t in a.GetTypes()
    where typeof(IDisposable).IsAssignableFrom(t)
    select t;
Thomas Levesque
This piece of code above, scans all the loaded assemblies (instead of just the executing assembly) and gives all disposable types sorted by namespace, so it is very easy to check for any disposable type that you have missed.Tx very much :)
apoorv020
Why was this answer downvoted ??
Thomas Levesque
+2  A: 

You can use NDepend to analyze your project and find all types that implement IDisposable.

Here´s the the cql query for the result

SELECT TYPES WHERE Implement "System.IDisposable"
Jehof