Do I need to call MemoryStream.Close ?

  • Thread starter Thread starter Oleg Subachev
  • Start date Start date
O

Oleg Subachev

I use local to methods MemoryStreams.
Both read-only and read-write ones.

Do I need to call MemoryStream.Close for read-only MemoryStreams before
leaving the method ?

And what about read-write MemoryStreams ?
 
Hi,

I suggest you to close / dispose your memory stream, and also set memsrm
variable to null..
In some cases if you dont set memsrm to null, it will lock this file and you
could not access it again till you restart your PC.. (maby this happends
only to me).

Regards,
Josip Habjan, Croatia
URL: www.habjansoftware.com
 
I suggest you to close / dispose your memory stream, and also set memsrm
variable to null..
In some cases if you dont set memsrm to null, it will lock this file and you
could not access it again till you restart your PC.. (maby this happends
only to me).

My MemoryStreams are not linked with any files.
 
You should not have to set the variable to null. The only thing you
should need to do is call close/dispose. However, it's always a good
idea to set the variable to null so you know not to use it anymore.
 
Hi

It is good practice to close all the streams after using them. However,
MemoryStream is little bit different. Closing/Disposing this stream doesn't
do much resourcewise. It just marks the stream as closed so no more
reading/writing to the stream are possible. Methods like ToArray() and
GetBuffer() still works, which means that the data is still in the memory.
The Dispose simply calls Close so it doesn't make much of a difference.

Depending on the type of the variable that holds the reference to the stream
(whether it is a local variable or class field), the level of memory
consumptions and the structure of the code that uses this variable it make
become more important to set the variable to *null* than to close/dispose
the stream.

But anyways it is always a good programming practice to close/dispose all
the streams when finish working with them.
 
Oleg,

Some people in this thread have suggested that you set the variable to
null. You actually don't want to do this. In debug builds, you will end up
keeping the reference around longer (because of lack of optimizations) than
necessary. In release builds, assignments to null should be optimized out.

Also, it is good practice to always call Dispose on any instance of a
type that implements IDisposable.

Hope this helps.
 
Nicholas said:
Also, it is good practice to always call Dispose on any instance of a
type that implements IDisposable.

If you are the one "responsible" for that object. For example:

void foo(Stream s) {
s.Write(...);
// should usually *not* invoke s.Dispose();
}

whereas the creator of the object should (usually) either: Dispose() it
when done:

void bar() {
using ( MemoryStream s = new MemoryStream() ) {
foo(s);
...;
}
}

or be sure it has given that responsibility to some other code:

class Baz: IDispoable {
Stream s;
void Dispose() { if ( s != null ) s.Dispose(); }
}
void quux(Baz baz) {
baz.s = new MemoryStream();
}
 
Helge,

Yes, if you are responsible for that. Sorry, I assumed that was
implied, but thanks for the clarification.
 
Josip Habjan said:
I suggest you to close / dispose your memory stream, and also set memsrm
variable to null..
In some cases if you dont set memsrm to null, it will lock this file and you
could not access it again till you restart your PC.. (maby this happends
only to me).

1) MemoryStreams aren't associated with any files
2) Closing a memory stream doesn't actually do anything, although it's
good practice to get into the habit of disposing of all IDisposable
objects where appropriate (i.e. where you own it)
3) Setting a variable to null will have no effect if the stream has
already been disposed; it's very rarely worth setting a variable to
null for garbage collection reasons, to be frank.
 
Back
Top