Is there an FxCop rule for using IDisposable locally?

... if I use IDisposable in a local variable, but do not call Dispose () or use the using () pattern.

public void BadMethod() { var fs = new FileStream("file.txt", FileMode.Create); fs.WriteByte(0x55); // no dispose, no using() } 

In the same way as "Types that have their own disposable fields must be disposable" for fields.


EDIT: Replaced MemoryStream with FileStream, because MemoryStream simply allocates memory and does not use (unmanaged) resources, so someone might discuss the mandatory call to Dispose ().

+6
c # idisposable fxcop
source share
1 answer

Is there an FxCop rule for this? Yes and no.

In FxCop 1.35, which is based on Visual Studio 2005 code analysis, there was a DisposeObjectsBeforeLosingScope rule that did just that.

In FxCop 1.36 (Visual Studio 2008 code analysis), they removed the data flow analysis engine, which meant that this rule also needed to be removed.

However, in the following FxCop code analysis ( Visual Studio 2010 ), it seems that DisposeObjectsBeforeLosingScope is back!

+15
source share

All Articles