Is there a FxCop rule for local used IDisposable's?

China☆狼群 提交于 2019-12-23 09:07:43

问题


... if I use an 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()
}

Just like the "Types that own disposable fields should be disposable" rule for fields.


EDIT: Replaced MemoryStream by FileStream, because MemoryStream just allocates memory and doesn't use (unmanaged) resources, so someone could discuss about a mandatory Dispose() call.


回答1:


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

In FxCop 1.35, which is what Visual Studio 2005 Code Analysis is based on, there was a rule DisposeObjectsBeforeLosingScope which did exactly this.

In FxCop 1.36 (Visual Studio 2008 Code Analysis), they removed their data flow analysis engine, which meant that this rule also had to be removed.

However, in the next FxCop (Visual Studio 2010 Code Analysis), it seems that DisposeObjectsBeforeLosingScope has returned!



来源:https://stackoverflow.com/questions/2214605/is-there-a-fxcop-rule-for-local-used-idisposables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!