MemoryStream 必须显式处置吗?

发布于 2024-10-02 22:52:14 字数 312 浏览 6 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城°AllureLove 2024-10-09 22:52:14

一般来说,所有一次性物品都必须被丢弃。

但是,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 a byte[] and an int)
The only reason it's disposable in the first place is that it inherits the abstract Stream class, which implements IDisposable.

Note that every other stream must be disposed.

尘世孤行 2024-10-09 22:52:14

任何实现 IDisposable 的类型都应该通过 try/catch/finally 块或通过 using 语句显式调用 Dispose

在某些情况下,从技术上讲,MemoryStream 不需要释放,但是为了遵守接口并保护自己免受下游更改的影响,仍然应该调用 Dispose

Any type that implements IDisposable should have Dispose 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 downstream Dispose should still be called.

人间☆小暴躁 2024-10-09 22:52:14

MemoryStream 实现了 IDisposable,因此如果可能,请使用 using 语句。

如果这不可行,请将其设为 try/catch/finally 块。

如果您需要让对象超出代码范围(当使用或 try/catch/finally 不起作用时),则调用者有责任实现显式处置。

MemoryStream implements IDisposable 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.

Smile简单爱 2024-10-09 22:52:14

请参阅此处避免使用语句出现问题

查看了 IL 和 using 这样做:

try
{
}finally
{
((System.IDisposable)obj).Dispose();
}

这意味着你的流将被处理,除了异常(如果它发生在
try 块)将保留在堆栈中,因此如果您不小心,它可能会导致您的应用程序崩溃。

所以:
“因此,MemoryStream 对象上的引用丢失了。这种情况是否需要 try/finally 块(或 using 语句)?” - 它是一样的

现在,如果 Dispose 方法由于某种原因失败会发生什么,这真的很有趣 -
你有一个 IE 安全漏洞:) 开玩笑:)

See here Avoiding Problems with the Using Statement

Looked at the IL and using does this:

try
{
}finally
{
((System.IDisposable)obj).Dispose();
}

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:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文