StreamWriter closes MemoryStream???

  • Thread starter Thread starter Bob Rock
  • Start date Start date
B

Bob Rock

Hello,

I've noticed that after calling the Close method on a StreamWriter, I get
exceptions on any operation I might request on the associated stream (with a
message "Cannot access a closed Stream"). Is this the behaviour I must
expect? Nothing is said on the documentation.


Bob Rock
 
Bob Rock said:
I've noticed that after calling the Close method on a StreamWriter, I get
exceptions on any operation I might request on the associated stream (with a
message "Cannot access a closed Stream"). Is this the behaviour I must
expect? Nothing is said on the documentation.

Yes, it's expected behaviour - but I agree it should be more clearly
documented. The StreamWriter is basically a wrapper, and when you close
the wrapper, it closes the wrapped stream as well. The same goes for
Dispose.

It is *sort* of documented - it says that Close is the same as
Dispose(true) and that "This [Dispose] method invokes the Dispose
method of each referenced object."
 
When calling Close,
the underlying System.IO.Stream is closed.
You should only call Close() on that base stream, when you dont need it
more.
 
Yes, it's expected behaviour - but I agree it should be more clearly
documented. The StreamWriter is basically a wrapper, and when you close
the wrapper, it closes the wrapped stream as well. The same goes for
Dispose.

It is *sort* of documented - it says that Close is the same as
Dispose(true) and that "This [Dispose] method invokes the Dispose
method of each referenced object."

Well, one thing that the documentation says is "You must call Close to
ensure that all data is correctly written to the underlying stream".
Yeah, sure, unfortunately I don't see what you can do with a closed stream.


Bob Rock
 
Bob Rock said:
It is *sort* of documented - it says that Close is the same as
Dispose(true) and that "This [Dispose] method invokes the Dispose
method of each referenced object."

Well, one thing that the documentation says is "You must call Close to
ensure that all data is correctly written to the underlying stream".
Yeah, sure, unfortunately I don't see what you can do with a closed stream.

The point is usually that when you've finished writing to a stream,
you're done with it anyway. In the case of MemoryStream, you can still
call ToArray.

It would certainly be nice to be able to do a "detach" on a
StreamWriter, saying that you no longer need its services with the
stream (and flush, please) but that the stream itself needs to live on.
 
I wrote a section of code at work today to write an XML string to a memory
stream with a streamwriter. After filling the memory stream I closed the
streamwriter and then deserialize my object from the memory stream. It
worked just fine so there must be at least some circumstances where it is
not the intended behavior to close the associated stream when the
streamwriter closes.

Dale
 
DalePres said:
I wrote a section of code at work today to write an XML string to a memory
stream with a streamwriter. After filling the memory stream I closed the
streamwriter and then deserialize my object from the memory stream. It
worked just fine so there must be at least some circumstances where it is
not the intended behavior to close the associated stream when the
streamwriter closes.

Dale

Dale,

would you post the "essential" part of the above code so that I may analyze
that you are doing "differently" to have the MemoryStream instance not
closed?
Thx.


Bob Rock
 
Back
Top