是内存泄漏创建如果在.NET中一个MemoryStream没有关闭? [英] Is a memory leak created if a MemoryStream in .NET is not closed?

查看:511
本文介绍了是内存泄漏创建如果在.NET中一个MemoryStream没有关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

MemoryStream foo(){
    MemoryStream ms = new MemoryStream();
    // write stuff to ms
    return ms;
}

void bar(){
    MemoryStream ms2 = foo();
    // do stuff with ms2
    return;
}

有没有机会,我已经分配的MemoryStream会以某种方式无法处置以后呢?

Is there any chance that the MemoryStream that I've allocated will somehow fail to be disposed of later?

我有一个同行评审坚持我手动关闭此,我无法找到的信息,告诉他有一个有效的点或没有。

I've got a peer review insisting that I manually close this, and I can't find the information to tell if he has a valid point or not.

推荐答案

如果事情是一次性的,你应该总是处置它。你应该用你的酒吧()方法using语句,以确保MS2得到处理。

If something is Disposable, you should always Dispose it. You should be using a using statement in your bar() method to make sure ms2 gets Disposed.

这最终将得到清理的垃圾收集器,但它始终是很好的做法处置。如果您在code运行的FxCop,它会标记它作为一个警告。

It will eventually get cleaned up by the garbage collector, but it is always good practice to Dispose. If you run FxCop on your code, it would flag it as a warning.

这篇关于是内存泄漏创建如果在.NET中一个MemoryStream没有关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆