How to satisfy CA2202 (do not delete objects several times)

This question may seem to you duplicated by CA2202, how to solve this case , which has an accepted answer. But you can understand that the accepted answer has 5 votes based on low quality. Also, any other voice responses do not really solve the problem. Most of them explain how to suppress a rule or debate about how this rule is wrong and why we should ignore it. Since this rule exists, there must be a way to satisfy it, and I am looking for community support to solve this problem.

I am trying to figure out how to satisfy CA2202 in the following code. I understand that the problem is here: using the operator also provides an encryptedStream object. But if I delete the finally part, it will start throwing CA2000

So what is the correct way to write it according to CA2202 and CA2000

byte[] result; MemoryStream encryptedStream = null; try { encryptedStream = new MemoryStream(); using (var cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)) { cryptStream.Write(inputInBytes, 0, inputInBytes.Length); cryptStream.FlushFinalBlock(); result = encryptedStream.ToArray(); } } finally { encryptedStream?.Dispose(); } string output = Convert.ToBase64String(result); 
+4
source share
2 answers

This is the literal answer to your question, because it will not issue CA warnings without suppressing them, and will only ever call Dispose once:

 MemoryStream encryptedStream = null; CryptoStream cryptStream = null; try { encryptedStream = new MemoryStream(); cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write); cryptStream.Write(inputInBytes, 0, inputInBytes.Length); cryptStream.FlushFinalBlock(); result = encryptedStream.ToArray(); } finally { if (cryptStream != null) { cryptStream.Dispose(); } else { if (encryptedStream != null) encryptedStream.Dispose(); } } string output = Convert.ToBase64String(result); 

But any developer who deserves their salt should take a look at this and go, "hmm, he doesn't seem to know using , I better rewrite this." Do not do this in production code. Suppress warning. Getting code like this correct (and if it remains correct in the face of change) is actually more difficult than writing code that uses using to suppress false warnings (indeed, I'm not quite sure that the code above is correct!). This primarily strikes the whole static code analysis: write reliable code. You should see code analysis as a tool, not an arbiter of correctness.

+6
source

Really, really, suppress the warning. The warning is incorrect. This is normal to do. :-)

Do a Google search for "nested use of CA2202" and you will find dozens of stack messages and MSDN messages on this issue. https://www.google.com/search?q=ca2202+nested+using

Ultimately, CA2202 and CA2000 are limited because they cannot understand the behavior of nested IDisposable objects. Some threads can be configured to leave the main thread open, but this usually does not happen. Suppression is indeed the right decision. The problem is that you are trying to be a good citizen, so you are trying to follow the warnings given to you. But static analysis is simply not smart enough to handle this.

+2
source

Source: https://habr.com/ru/post/1270787/


All Articles