Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code analysis - Do not dispose object multiple times

i have tried following the rules of code analysis on this method:

public static string Encrypt(string password)
{
    string myPassword = string.Empty;
    if (!string.IsNullOrEmpty(password))
    {
        myPassword = password;
        byte[] Value = System.Text.Encoding.UTF8.GetBytes(myPassword);
        SymmetricAlgorithm mCSP = new RijndaelManaged();
        mCSP.Key = _key;
        mCSP.IV = _initVector;
        using (ICryptoTransform ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV))
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
                {
                    cs.Write(Value, 0, Value.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                    myPassword = Convert.ToBase64String(ms.ToArray());
                }
            }
        }
    }
    return myPassword;
}

added all the Try {} Finaly{} blocks, but it was still yelling at me that i dont respect rule 2202. anyone can give me a hand with this?

yes, i have read other posts about this subject and tried applying it, but at the end i still get the same message.

like image 573
Rafael Herscovici Avatar asked Feb 02 '12 09:02

Rafael Herscovici


People also ask

Can dispose be called multiple times?

A correctly implemented Dispose method can be called multiple times without throwing an exception. However, this is not guaranteed and to avoid generating a System. ObjectDisposedException you should not call Dispose more than one time on an object.

What will happen if we call Dispose () method directly?

The Dispose() methodThe Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.

Do I need to Dispose object in c#?

C# provides garbage collection and thus does not need an explicit destructor. If you do control an unmanaged resource, however, you will need to explicitly free that resource when you are done with it.

What is dispose in VB net?

Dispose improves performance and optimizes memory by releasing unmanageable objects and scarce resources, like Graphics Device Interface (GDI) handles used in applications with restricted Windows space. The Dispose method, provided by the IDisposable interface, implements Dispose calls.


1 Answers

To get rid of the CA2202 warning for cs, simply remove the call to its Close method.

The CA2202 problem for ms is a wee bit more complex. The warning is cropping up because CryptoStream has the effrontery to dispose the stream it received via is constructor, which means that there's one inappropriate call to ms.Close() that you can't avoid. The good news is that this untimely disposition has no side-effects in your case, and the same goes for the double disposition, so you can safely slap on a SuppressMessageAttribute and ignore the problem. (For cases where you actually need to passed stream to survive its unpreventable disposition by something like CryptoStream, the usual technique is to use a stream subclass whose disposition can be prevented by its instantiating code.)

like image 158
Nicole Calinoiu Avatar answered Sep 21 '22 15:09

Nicole Calinoiu