StreamWriter closes MemoryStream???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Rock

    #1

    StreamWriter closes MemoryStream???

    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


  • Jon Skeet [C# MVP]

    #2
    Re: StreamWriter closes MemoryStream???

    Bob Rock <nospam.yet_ano ther_apprentice @hotmail.com> wrote:[color=blue]
    > 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.[/color]

    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."

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Dennis Myrén

      #3
      Re: StreamWriter closes MemoryStream???

      When calling Close,
      the underlying System.IO.Strea m is closed.
      You should only call Close() on that base stream, when you dont need it
      more.

      "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
      news:%23NnOdhaO EHA.2844@tk2msf tngp13.phx.gbl. ..[color=blue]
      > 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[/color]
      a[color=blue]
      > message "Cannot access a closed Stream"). Is this the behaviour I must
      > expect? Nothing is said on the documentation.
      >
      >
      > Bob Rock
      >
      >[/color]


      Comment

      • Bob Rock

        #4
        Re: StreamWriter closes MemoryStream???

        > Yes, it's expected behaviour - but I agree it should be more clearly[color=blue]
        > 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."[/color]

        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


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: StreamWriter closes MemoryStream???

          Bob Rock <nospam.yet_ano ther_apprentice @hotmail.com> wrote:[color=blue][color=green]
          > > 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."[/color]
          >
          > 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.[/color]

          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.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • DalePres

            #6
            Re: StreamWriter closes MemoryStream???

            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


            "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
            news:%23NnOdhaO EHA.2844@tk2msf tngp13.phx.gbl. ..[color=blue]
            > 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[/color]
            a[color=blue]
            > message "Cannot access a closed Stream"). Is this the behaviour I must
            > expect? Nothing is said on the documentation.
            >
            >
            > Bob Rock
            >
            >[/color]


            Comment

            • Bob Rock

              #7
              Re: StreamWriter closes MemoryStream???

              "DalePres" <nospam@nomail. com> wrote in message
              news:uVMFMgiOEH A.1620@TK2MSFTN GP12.phx.gbl...[color=blue]
              > 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[/color]

              Dale,

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


              Bob Rock


              Comment

              Working...