1
 Using ms As New MemoryStream
    Dim st As New GZipStream(ms, CompressionMode.Compress, True)

    '... some code

    Return buffer
 End Using

And this:

dim As New MemoryStream
 using st As New GZipStream(ms, CompressionMode.Compress, True)

    '... some code

    Return buffer
 End Using

I had some legacy code which called Dispose() and Close() manually on the stream objects - this was causing CA2202 code analysis warnings about possible multiple calls to Dispose(). So I added using statements for both memory stream and gzipstream and the error didn't go away!?

If I used it on either the memorystream or gzipstream objects then the error then went away. Was is causing this behaviour?

jaffa
  • 26,770
  • 50
  • 178
  • 289

1 Answers1

0

The reason may be, because the GZipStream is disposing of the underlying stream when its own Dispose method is called. Therefore, I'd recommend keeping the Using statement for the GZipStream only. At least, this is the behavior of most built-in stream wrappers in the .NET framework. For instance BufferedStream will close the underlying stream object when Close()/Dispose() is invoked (as per the MSDN documentation, behavior is explained in a comment in the code sample)

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108