问题描述
我有以下的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没有关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!