- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何正确处理 File.OpenRead()。我目前正在使用以下代码?
using (BinaryReader br = new BinaryReader(File.OpenRead(path)))
{
myByte = br.ReadByte();
}
我在分析代码时在 Visual Studio 中得到以下信息:
Warning 1 CA2000 : Microsoft.Reliability : In method 'Program.Main(string[])', object 'File.OpenRead(path)' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'File.OpenRead(path)' before all references to it are out of scope.
最佳答案
乍一看,这看起来像是误报,因为处置 BinaryReader
也会处置 File.OpenRead
返回的 FileStream
:
发件人:http://msdn.microsoft.com/en-us/library/azy2k2bx.aspx
When the disposing parameter is true, this method releases all resources held by any managed objects that this BinaryReader references. This method invokes the Dispose method of each referenced object.
但是,有一个极端情况,FileStream
确实没有被释放:当 BinaryReader
的构造函数抛出异常时!
解决方案:
编写代码的正确方法是这样的:
using (var fs = File.OpenRead(path))
{
BinaryReader br = new BinaryReader(fs);
myByte = br.ReadByte();
}
背景:BinaryReader
仅包含对 FileStream
的引用,因此不需要释放。
Code Analysis 同意这一观点。
顺便说一句:将此解决方案用于可写流时,在流被释放之前刷新编写器很重要:
using (var fileStream = new FileStream(...))
{
var writer = new StreamWriter(fileStream);
writer.WriteLine(...);
writer.Flush(); // <-- Important
}
如果您忘记了这一点,您的流可能不会包含使用 StreamWriter
编写的所有内容。
关于c# - 如何处置 File.OpenRead(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5462203/
释放 MKMapView 后,我遇到了奇怪的(?)崩溃。 MKMapView 是我的 View Controller 中的 subview ,在我从导航堆栈中删除该 View 并释放它后,应用程序崩溃
我正在尝试使用以下方法处理我的 JFrame: private void killJFrame(JFrame jFrame) { SwingUtilities.invokeLater(() -
我用的是asp net 4.5。 我有 Marker.aspx 页面和页面 Marker.aspx.cs 背后的代码。每当回发发生时,Page_Load 函数将在代码隐藏中触发并创建 GeoMarku
在 C# 中,如 Documentation 中所述, 和 this nice post接受的答案,声明类不继承其父类的析构函数。 问题:如果我想确保释放基类的私有(private)元素,在所有子类中
我想知道是否有人知道如何知道 InheritedWidget 何时被释放? 这个问题的原因是我正在做一些实验,并且我正在使用 InheritedWidget 作为 BLoC 的提供者。此 BLoC 在
如果应用程序意外关闭,如何安全地处理 ReportViewer 对象 Public Shared rv As New Microsoft.Reporting.WinForms.ReportViewe
有没有办法销毁 WebView 实例?如果页面加载,并说视频开始播放,我希望能够,当我隐藏 WebView 时,基本上可以销毁它,或者至少重置它。 我知道我可以听 visibleProperty 并执
在总体情况下,Close 方法在语义上只是更改对象的状态,而该对象可以使用 Open 方法无限期地再次更改。 另一方面,IDisposable.Dispose() 方法的语义将对象置于无法撤消的状态。
我使用StreamResourceInfo.Stream从资源中获取BitmapImage。使用流后 Close 和 Dispose 是否正确?我问这个问题是因为在内存分析器中,如果这样做我会收到错误
我正在编写一个程序,显示在屏幕上移动的图像,但是对于作业,我必须使用drawImage方法。我创建一个名为turtle的新图像,然后在一个点绘制该图像,然后在稍后的时间点再次绘制它,但是,第一个绘制的
这个问题在这里已经有了答案: Do you need to dispose of objects and set them to null? (12 个答案) 关闭 4 年前。 我在我的代码中使用
我有一个使用 Entity Framework 的 MVC 3 应用程序,我在其中设置了自定义角色提供程序。 我的角色提供者依赖于一个存储库,而该存储库依赖于 DbContext。 我在应用程序启动方
我正在尝试创建处理整个 BST 的迭代方法。 通过函数 insert_nodes 插入节点后,我没有得到预期的输出。 它应该打印如下内容:left,right, #nr #nr #nr 对于数字 5,
我正在使用 MemoryAppender 来读取单元测试中的日志消息。 我按以下方式使用 BasicConfigurator: class LogVerifier { pr
谁能给出一个完整的例子来说明 qooxdoo 1.6 中的 dispose 和 destruct 是如何工作的? ,我在 qooxdoo 演示或文档中找不到任何好的示例。 谢谢你的建议。 最佳答案 处
我在处理屏幕时遇到问题。当我尝试处置 OrthogonalTiledMapRenderer 时,收到此错误消息。我在网上查了一下,没有发现任何其他这样的例子或发生这种情况的情况。 Exception
我正在使用匿名方法来处理 COM 对象中的事件。程序终止后,我在匿名方法中使用的资源似乎没有被“正确关闭”,因为我正在观看的每个资源都会出现第一次机会异常 (InvalidComObjectExcep
我有一个用 C# 编写的方法,它接受一个包含 XML 文档的字符串和一个 XSD 流数组。字符串文档根据 XSD 进行验证: private static XmlValidationResult Va
有没有办法“清理”您创建的对象和其他变量?还是它们会自动处理掉,或者我的整个概念都错了?这样做的正确方法是什么?我尽量避免 GC。 最佳答案 在没有内存管理的 GC 语言中,唯一的清理方法是 GC 。
当您在 ASP.Net 框架中创建和使用 Web 服务代理类时,该类最终继承自 Component,后者实现了 IDisposable。 我从未在网上看到过人们处理 Web 代理类的例子,但想知道是否
我是一名优秀的程序员,十分优秀!