Get rid of CA2202

How can I get rid of the CA2202 warning (CA2202 : Microsoft.Usage : Object 'compressedStream' can be disposed more than once in method 'Compression.InternalDecompress(byte[])'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object) from the following code:

        using (var compressedStream = new MemoryStream(inputData))
        using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
        using (var resultStream = new MemoryStream())
        {
            zipStream.CopyTo(resultStream);
            return resultStream.ToArray();
        }

I have tried getting rid of the "using" statement and replacing it with try/finally pattern but then I get CA2000 (CA2000 : Microsoft.Reliability : In method 'Compression.InternalDecompress(byte[])', call System.IDisposable.Dispose on object 'stream' before all references to it are out of scope). I have tried replacing the above code like this:

        MemoryStream decompressedData = null;
        MemoryStream stream = null;
        GZipStream decompressor = null;
        try
        {
            decompressedData = new MemoryStream();
            stream = new MemoryStream(inputData);
            decompressor = new GZipStream(stream, CompressionMode.Decompress, false);
            stream = null;

            int bytesRead = 1;
            int chunkSize = 4096;
            byte[] chunk = new byte[chunkSize];

            while ((bytesRead = decompressor.Read(chunk, 0, chunkSize)) > 0)
            {
                decompressedData.Write(chunk, 0, bytesRead);
            }

            decompressor = null;

            return decompressedData.ToArray();
        }
        finally
        {
            if (stream != null)
            {
                stream.Dispose();
            }

            if (decompressor != null)
            {
                decompressor.Dispose();
            }

            if (decompressedData != null)
            {
                decompressedData.Dispose();
            }
        }
728x90

3 Answers Get rid of CA2202

This is what i use:

public class Compression
{
    public Compression()
    {

    }

    public byte[] Compress(byte[] buffer)
    {
        byte[] gzBuffer;

        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                zip.Write(buffer, 0, buffer.Length);
                zip.Close();
            }
            ms.Position = 0;

            MemoryStream outStream = new MemoryStream();

            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);

            gzBuffer = new byte[compressed.Length + 4];
            Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
        }

        return gzBuffer;
    }

    public byte[] Decompress(byte[] gzBuffer)
    {
        byte[] buffer;

        using (MemoryStream ms = new MemoryStream())
        {
            int msgLength = BitConverter.ToInt32(gzBuffer, 0);
            ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

            buffer = new byte[msgLength];

            ms.Position = 0;
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
            {
                zip.Read(buffer, 0, buffer.Length);
            }
        }

        return buffer;
    }
}

Or you can just add a pragma statement to your class

#pragma warning disable 2202

namespace Your.Namespace
{
...
}

#pragma warning restore 2202

7 year ago

This is what I ended up using - gets rid of both CA2000 and CA2202:

    private static MemoryStream GetMemoryStream()
    {
        return new MemoryStream();
    }

    private static byte[] InternalDecompress(byte[] inputData)
    {
        Debug.Assert(inputData != null, "inputData cannot be null");

        MemoryStream decompressedData = GetMemoryStream();
        MemoryStream inputDataMemoryStream = GetMemoryStream();

        GZipStream decompressor = null;

        try
        {
            inputDataMemoryStream.Write(inputData, 0, inputData.Length);
            inputDataMemoryStream.Position = 0;

            decompressor = new GZipStream(inputDataMemoryStream, CompressionMode.Decompress, false);

            int bytesRead;
            int chunkSize = 4096;
            byte[] chunk = new byte[chunkSize];

            while ((bytesRead = decompressor.Read(chunk, 0, chunkSize)) > 0)
            {
                decompressedData.Write(chunk, 0, bytesRead);
            }
        }
        finally
        {
            if (decompressor != null)
            {
                decompressor.Dispose();
            }
        }

        return decompressedData.ToArray();
    }

7 year ago

This is my attempt. It works and it avoids CA2202

    /// <summary>
    /// Compresses byte array to new byte array.
    /// </summary>
    public byte[] Compress(byte[] raw)
    {
        MemoryStream outStream = null;
        GZipStream tinyStream = null;
        byte[] retValue = null;
        try
        {
            outStream = new MemoryStream();
            tinyStream = new GZipStream(outStream, CompressionMode.Compress);
            using (var mStream = new MemoryStream(raw))
                mStream.CopyTo(tinyStream);
        }
        finally
        {
            if (tinyStream != null)
            {
                tinyStream.Dispose();
                retValue = outStream.ToArray();
            }
            else if (outStream != null)
            {
                retValue = outStream.ToArray();
                outStream.Dispose();
            }
        }
        return retValue;
    }

3 year ago