Is calling MemoryStream.ToArray() dangerous after disposing?

No, there's no chance of that. It's safe to do - the MemoryStream keeps a strong reference to the byte array.

I'll see if I can find any documentation about guarantees...

EDIT: Sort of...

From MemoryStream.Close:

The buffer is still available on a MemoryStream once the stream has been closed.

Admittedly that doesn't guarantee it for Dispose, but that's documented to call Stream.Close.

MemoryStream.Dispose(bool) could then be overridden to release the array, but it doesn't in my experience, and it would be a breaking change at this point.


Comments

  1. Foster

    • 2021/7/24

    . It's safe to do - the MemoryStream keeps a strong reference to the byte array.

  2. Joseph

    • 2017/5/24

    1 Answer1. Active Oldest Votes. 28. No, there's no chance of that. It's safe to do - the MemoryStream keeps a strong reference to the byte array. I'll see if I can find any documentation about guarantees EDIT: Sort of From MemoryStream.Close: The buffer is still available on a MemoryStream once the stream has been closed.

  3. Ramos

    • 2019/3/25

    ToArray() dangerous after disposing? In the below code, is there any chance the GC will clean out the MemoryStream so that ToArray will fail, since it is 

  4. Alberto

    • 2020/4/26

    When CryptoStream is disposed (when the using block ends), it calls Close on the MemoryStream, which calls Dispose. So, you don't need to call Dispose (via using) or Close yourself. This way, stream only gets disposed exactly once. MemoryStream stream = new MemoryStream(); using (CryptoStream cs = new CryptoStream(stream, transform, CryptoStreamMode.Write)) { cs.Write(buffer, 0, buffer.Length); return stream.ToArray(); }

  5. Leone

    • 2016/7/17

    The following code example shows how to read and write data using memory as a This means that disposing it by directly calling Dispose() or by using a 

  6. Bonnet

    • 2017/2/27

    Byte array is still available after closing/disposing the stream then call CloudBlobContainer.Create() Is calling MemoryStream.ToArray() dangerous after

  7. Leonardo

    • 2015/3/13

    ToArray() is explicitly usable after MemoryStream.Dispose(); that's called out in the docs and a non-trivial amount of code relies on that 

  8. Denver

    • 2020/1/4

    One possible solution would be to keep track of all the MemoryStream instances and dispose of them after they are done being used. This is the code for that: This is the code for that:

  9. Leonel

    • 2020/1/9

    To see this (if you are on 64 bit), target the following program at MemoryTributary includes the method ToArray() but this is unsafe as 

  10. Maximus

    • 2019/10/21

    Not disposing a MemoryStream is not misuse, nor is using ToArray after disposal. Concurrent use is misuse, but at the same time, MemoryStream is used by millions of libraries and applications; there is guaranteed to be misuse.

  11. Edward

    • 2018/9/5

    Create a file called test.txt in the current directory: using (Stream s = new ReadMessage (PipeStream s) { MemoryStream ms = new MemoryStream(); byte[] 

  12. Orlando

    • 2018/9/3

    Is calling MemoryStream.ToArray() dangerous after disposing? UInt32.TryParse() hex-number not working . Anonymous inner classes in C# . C#: Elegant code for getting a

  13. Wright

    • 2019/12/7

    On my test system (described later), this takes about 272 milliseconds! We know it's important to minimize the number of read() calls 

  14. Ali

    • 2017/10/23

    Here are the examples of the csharp api class System.IO.MemoryStream.ToArray() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

  15. Adriel

    • 2018/11/18

    If more security is required, you should extend this class to allow for certificates or some other form of encryption. If you've been following 

Comments are closed.

Recent Posts