7

I am looking for memory leaks in a huge code base so going line by line and observing for every possible location of where an IDisposable is used without being put in a using statement or without being disposed is not an option.

I am currently using NDepend with the query from this answer NDepend CQL Query for missing IDisposable implementation but that is not what I need. I need to know if an object is instantiated and later on not being disposed of. How to write a query in NDepend that will find those objects? Or if that is not possible then how to get a list of places where IDisposable objects are being instantiated?

Community
  • 1
  • 1
Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam." – Sebastian Negraszus Mar 05 '15 at 15:07
  • 3
    I would recommend using some memory profiler instead (Dynatrace, ANTS memory profiler, etc.). Its way more effective for debugging this kind of issues than static code analysis, unfortunately the better profilers aren't free. – Ondrej Svejdar Mar 05 '15 at 15:08
  • Ok, I've changed the question to be more specific – Mihail Shishkov Mar 05 '15 at 15:12
  • @OndrejSvejdar I've used RedGate's and JetBrains profiler and found some problems. What I need now is to make sure the code passes some quality requirements. – Mihail Shishkov Mar 05 '15 at 15:15
  • NDepend is good for static analysis, but doesn't have the necessary analysis capability to determine whether something at runtime is going to be disposed. – Steve Mitcham Mar 05 '15 at 15:15
  • 3
    As an aside: an `IDisposable` that isn't disposed is not a memory leak. It merely delays finalization of resources. This can be bad enough in and of itself, but a true memory leak is one where objects are created and mistakenly kept referenced (disposable or not). It is probably even less feasible to detect something like this with a static checker than tracking `IDisposable`s. – Jeroen Mostert Mar 05 '15 at 15:19
  • @JeroenMostert I agree but this is another place to look at so I have to verify that too :) – Mihail Shishkov Mar 05 '15 at 15:23
  • @JeroenMostert: Failure to dispose something might merely delay finalization, but failure to dispose a short-lived object that receives events from a long-lived source may extend its lifetime--and that of anything to which it holds references--to that of the source. If an unbounded number of such objects may attach to a source, failure to dispose them would constitute a memory leak, and could also prevent cleanup of other resources which would otherwise have been finalized. – supercat Mar 05 '15 at 17:06
  • We advise using a memory profiler indeed, like RedGate ANTS Memory profiler or Jetbrain dotTrace Memory. Static analysis can help finding easy not-disposable mistake, but for complex ones, a dynamic profiler is needed. – Patrick from NDepend team Mar 06 '15 at 05:35

1 Answers1

3

Take a look at this: CA2000: Dispose objects before losing scope

milen.vichev
  • 122
  • 1
  • 7