I have issues satisfying a code analysis rule for disposing a MemoryStream
object.
This is my code right now:
byte[] bytes;
MemoryStream stream = new MemoryStream();
using (CryptoStream cs = new CryptoStream(stream, transform, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
bytes = stream.ToArray();
stream.Close();
return bytes;
This results in a warnings that the stream may be disposed twice, or may not be disposed (one warning for each).
I have also tried wrapping it into a using(MemoryStream stream = new MemoryStream())
block. This results in the former warning.
And finally, removing the calls to Close()
or Dispose()
results in the latter warning.
Is there a way to satisfy both conditions? I assume the problem is an exception path that may close it, but I am not very familiar with how these classes work.