Question

I recently downloaded Visual Studio 2013 and I ran the Code Analysis on a project I'm working on. Its thrown up a couple of issues that I'm working through but one in particular is about how I am using the "using" IDisposable statement.

Here's an example of my code:

using (MemoryStream msDecrypt = new MemoryStream(encryptedText.ToBase64Byte()))
{
    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
    {
        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
        {
            return srDecrypt.ReadToEnd();
        }
    }
}

I understand the Analysis warning is trying to tell me that disposing of multiple objects in this way could throw an object disposed issue.

I'm sure that disposing on one object is not going to throw an exception in the case above. So should I modify my code or keep as is?

Was it helpful?

Solution

There should be no issue with your code as far as I can tell, I have always used nested using statements in the past.

From looking through other questions I believe the problem is with the code analysis tool itself and both CA2000 and CA2202 rules. False positives often occur when using various stream and reader types with using statements.

You should ignore the warnings and continue as alternative methods (such as try/finally) will produce bad code and your code is valid.

OTHER TIPS

The reason From MSDN

Nested using statements (Using in Visual Basic) can cause violations of the CA2202 warning. If the IDisposable resource of the nested inner using statement contains the resource of the outer using statement, the Dispose method of the nested resource releases the contained resource. When this situation occurs, the Dispose method of the outer using statement attempts to dispose its resource for a second time.

in your case though. it is fine to use it like this. So, you should just suppress the warning.

OR

you can just go the ugly way of suppressing the warning by modifying your code by implementing Try/finally. Although, you should really avoid that.

I don't see any issue about using the multiple using statements to dispose the objects. It's common to have nested using statements and your code is valid.

Using objects of different types, you can nest using the one-line syntax if you want to save space and improve readability:

using (MemoryStream msDecrypt = new MemoryStream(encryptedText.ToBase64Byte()))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
     return srDecrypt.ReadToEnd();
}

The issue you encountered is recurrent using the Code Analysis (you can find the cause of the warning here). You can then ignore the warning about the multiple using statements, because this is a false positive and your code is fine and valid.

You can also suppress the warning in this way (thanks to @Jordão), as described in this nice answer:

[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public void MyMethodWithUsings()
{
     using (MemoryStream msDecrypt = new MemoryStream(encryptedText.ToBase64Byte()))
     using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
     using (StreamReader srDecrypt = new StreamReader(csDecrypt))
     {
           return srDecrypt.ReadToEnd();
     }
}

If you want to remove the warning, you'll have to rewrite your code using the block try/finally and call the Dispose() method of your objects in the finally block, but this will produce awful and not easy to read code, as you can see in this question.

That's a recurrent issue with code analysis and both CA2000 and CA2202.

You have the possibility to fix those warnings by not using using, but using try/finally/dipose. That's not recommended because it produces awful code.

Your code will work, simply suppress those warnings.

Also see https://stackoverflow.com/a/21849534/870604 for StreamReader/Writer and underlying stream ownership.

For this type of warning you can easily ignore the warning, because it usually is a false positive. Check the following links for more information:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top