MemoryStream 必须显式处置吗?
由于 MemoryStream 是非托管资源,它是否总是需要被处置?
鉴于:
1) A method is invoked.
2) A MemoryStream object is created (MemoryStream ms = new MemoryStream();).
3) An exception occurs and is caught from the invoking classes.
MemoryStream 对象的引用因此丢失。这种情况是否需要 try/finally 块(或 using 语句)?
As MemoryStream is an unmanaged resource does it always have to be disposed?
Given:
1) A method is invoked.
2) A MemoryStream object is created (MemoryStream ms = new MemoryStream();).
3) An exception occurs and is caught from the invoking classes.
The reference on the MemoryStream object is therefore lost. Does this scenario need a try/finally-block (or using-statement)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,所有一次性物品都必须被丢弃。
但是,
MemoryStream
实际上不需要释放,因为它没有任何非托管资源。 (它只是一个byte[]
和一个int
)它首先是一次性的唯一原因是它继承了抽象
Stream
类,该类实现了IDisposable
。请注意,所有其他流都必须被释放。
In general, all disposable objects must always be disposed.
However,
MemoryStream
doesn't actually need to be disposed, since it doesn't have any unmanaged resources. (It's just abyte[]
and anint
)The only reason it's disposable in the first place is that it inherits the abstract
Stream
class, which implementsIDisposable
.Note that every other stream must be disposed.
任何实现
IDisposable
的类型都应该通过 try/catch/finally 块或通过 using 语句显式调用Dispose
。在某些情况下,从技术上讲,
MemoryStream
不需要释放,但是为了遵守接口并保护自己免受下游更改的影响,仍然应该调用Dispose
。Any type that implements
IDisposable
should haveDispose
called on it either explicitly via a try/catch/finally block or via the using statement.There are cases such as this where technically the
MemoryStream
does not need disposed, however to honor the interface and protect yourself from changes downstreamDispose
should still be called.MemoryStream
实现了IDisposable
,因此如果可能,请使用 using 语句。如果这不可行,请将其设为 try/catch/finally 块。
如果您需要让对象超出代码范围(当使用或 try/catch/finally 不起作用时),则调用者有责任实现显式处置。
MemoryStream
implementsIDisposable
so when possible, use a using statement.When that isn't feasible, make it a try/catch/finally block.
In cases when you need to let the object pass out of the scope of your code (when using or try/catch/finally won't work), it becomes the responsibility of the caller to implement the explicit disposal.
请参阅此处避免使用语句出现问题
查看了 IL 和
using
这样做:这意味着你的流将被处理,除了异常(如果它发生在
try 块)将保留在堆栈中,因此如果您不小心,它可能会导致您的应用程序崩溃。
所以:
“因此,MemoryStream 对象上的引用丢失了。这种情况是否需要 try/finally 块(或 using 语句)?” - 它是一样的
现在,如果 Dispose 方法由于某种原因失败会发生什么,这真的很有趣 -
你有一个 IE 安全漏洞:) 开玩笑:)
See here Avoiding Problems with the Using Statement
Looked at the IL and
using
does this:Which means your stream will get disposed no matter what but the exception(if it occurs in
the try block) will remain on the stack so it may crash you app if you don't take care.
So:
"The reference on the MemoryStream object is therefore lost. Does this scenario need a try/finally-block (or using-statement)?" - Its the same
Now its really interesting what would happen if the Dispose method fails for some reason-
you got an IE security hole:) Kidding:)