2012-02-20 13 views
3

CA2202警告(CA2202:Microsoft.Usage:Object 'compressedStream'は、メソッド 'Compression.InternalDecompress(byte [])'に複数回配置できます)を取り除くにはどうしたらいいですか? System.ObjectDisposedException次のコードから)オブジェクト上に複数の時間を設ける呼び出すべきではありません。CA2202を取り除く

 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(); 
     } 

私は「使用」の文とのtry /最後にパターンに置き換えることが、その後、私を退治しようとしていますCA2000(CA2000:Microsoft.Reliability: 'Compression.InternalDecompress(byte [])'メソッドで、すべての参照が範囲外になる前に 'stream'オブジェクトのSystem.IDisposable.Disposeを呼び出します)。私はこのような上記のコードを交換しようとしている:

 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(); 
      } 
     } 
+0

[この事件を解決する方法CA2202、](http://stackoverflow.com/questions/3831676/ca2202-how-to-solve-this-case) –

答えて

1

これは私が使用するものです。

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; 
    } 
} 

それとも、ただこれは、あなたのクラスに

#pragma warning disable 2202 

namespace Your.Namespace 
{ 
... 
} 

#pragma warning restore 2202 
+0

の可能な重複を避けますCA2202:Microsoft.Usage:Object 'ms'は、メソッド 'Compression.InternalDecompress(byte [])'に複数回配置することができます。System.ObjectDisposedExceptionを生成しないようにするには、次のように警告します。コールしないオブジェクトに対して複数回廃棄する。 – Ambuj

+1

これは単なる警告であるため、プラグマを追加すると警告が表示されないことがあります。上記の修正コードを参照してください。 –

+0

2016年にこれらの警告(特にCA2202)は実際には起こりそうな出来事を示さずに、ほとんど全てのUSING文に完全に擬似でフラグを立てているように見えます。 –

1

をプラグマステートメントを追加することができます私が使ったのは、CA2000と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(); 
    } 
0

これは私の試みです。それは動作し、それが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; 
    } 
関連する問題