There are many questions SO asking how to detect IDisposable objects leak. It seems like the answer is "you can't".
I just checked with the most trivial test case, that FxCop 10.0 doesn't do it, ReSharper 4 with MSVS2010 doesn't do it.
This seems wrong to me, worse than memory leaks in C (for which at least we have established tools to detect).
I was thinking: Is it possible, using reflection and other obscure advanced techniques, that I can inject a check at runtime, in the finalizer to see whether Dispose
has been called?
How about magic tricks with WinDBG+SOS?
Even if there aren't existing tools to do it, I'd like to know whether this is theoretically possible (my C# isn't very sharp).
Ideas?
NOTE This question's title might have been misleading. The real question here should be whether a IDisposable
object has been Disposed()
properly. Getting disposed by the GC doesn't count since I regard that as a mistake.
Edit: Solution: .NET Memory Profiler does the job. We just need to spam several GC.Collect()
at the end of the program to enable our profiler to correctly pick up the stats.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…