I was wondering if there was an addon that could detect and create a warning (or error) if it detects the creation of an IDisposable object that is never disposed.
I looked through Resharper's docs but didn't see anything that looked like what I was wanting.
edit - To be more specific, I should have said 开发者_高级运维a Local variable that implements IDisposable, but is never Disposed.
For instance, a Pen is created in a paint method, but not disposed with after the drawing is done.
This has been turned on and off a few times in FxCop. Its current state is on again, at least for the version that comes with VS2010. It generates CA2000 on this code:
protected override void OnPaint(PaintEventArgs e) {
var pen = new Pen(Brushes.Black);
}
Warning 5 CA2000 : Microsoft.Reliability : In method 'Form1.OnPaint(PaintEventArgs)', call System.IDisposable.Dispose on object 'pen' before all references to it are out of scope.
Beware that the reliability of this warning isn't great, it is a difficult problem to solve.
It's incredibly unlikely that a static analysis tool would be capable of showing that an object is or is not ever disposed in an arbitrary program. I believe that this would be effectively equivalent to solving the Halting problem.
精彩评论