Find not disposed IDisposables
Asked Answered
D

1

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?

Defamatory answered 5/3, 2015 at 15:5 Comment(9)
"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."Pouncey
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.Vanquish
Ok, I've changed the question to be more specificDefamatory
@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.Defamatory
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.Leukorrhea
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 IDisposables.Gies
@JeroenMostert I agree but this is another place to look at so I have to verify that too :)Defamatory
@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.Kathrinkathrine
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.Madeira
B
3

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

Backsaw answered 5/3, 2015 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.