我认为这是一种很好的做法,在使用 Assembly.GetManifestResourceStream
方法访问嵌入式程序集资源时,在完成返回的 Stream
后关闭它。然而,我刚刚在下面的文章中发现了一些东西:
http://msdn.microsoft.com/en-us/library/ms950960.aspx
// Get the stream that holds the resource
// NOTE1: Make sure not to close this stream!
// NOTE2: Also be very careful to match the case
// on the resource name itself
Stream stream =
assem.GetManifestResourceStream("Azul.jpg");
// Load the bitmap from the stream
this.BackgroundImage = new Bitmap(stream);
这里的评论说流应该不被关闭,尽管这篇文章没有提到为什么。在谷歌上搜索没有提供任何结论;有些人似乎关闭了这个流,有些人没有,并说垃圾收集器会处理它。
我应该关闭 Assembly.GetManifestResourceStream
返回的流吗?有什么我不应该这样做的特殊原因吗?
最佳答案
该评论不希望您关闭它,因为它继续从中创建一个 Bitmap
对象。
通常,您应该在使用完这些流后将其关闭,否则您的应用程序会发生内存泄漏。
关于c# - 我应该关闭 Assembly.GetManifestResourceStream 返回的流吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11906209/