title: Correct way to free/allocate the context in the OpenSSL
body: <p>I'am using the Open SSL in my program, to encrypt and decrypt the data using aes ciphers. At the moment there is a little memory leak, so i'm looking for a way to fix that. In my encrypt decrypt routines, i have the contexts free like so</p>
  
  <pre><code>EVP_CIPHER_CTX_free(ctx);
  </code></pre>
  
  <p>And created by:</p>
  
  <pre><code>EVP_CIPHER_CTX_new
  </code></pre>
  
  <p>This was on the OpenSSL wiki page in the <a href="http://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption">examples</a></p>
  
  <p>But! On the MAN page, there is a suggestion for using <code>EVP_CIPHER_CTX_cleanup</code> and <code>EVP_CIPHER_CTX_init</code> functions. So basically what should be correct to use, is the <code>EVP_CIPHER_CTX_new</code>/<code>EVP_CIPHER_CTX_free</code> is somehow deprecated? And is there any big difference between <code>EVP_CIPHER_CTX_new</code>/<code>EVP_CIPHER_CTX_free</code> and <code>EVP_CIPHER_CTX_init</code> / <code>EVP_CIPHER_CTX_cleanup</code> ?</p>
  
  <pre><code>if(!(ctx = EVP_CIPHER_CTX_new())) return -1;
  
  
    if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
    {
      EVP_CIPHER_CTX_free(ctx);
      return -1;
    }
  
    if(1 != EVP_EncryptUpdate(ctx, ciphertext, &amp;len, plaintext, plaintext_len))
    {
      EVP_CIPHER_CTX_free(ctx);
      return -1;
    }
    ciphertext_len = len;
  
  
    if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &amp;len)) { EVP_CIPHER_CTX_free(ctx); return -1; }
    ciphertext_len += len;
  
  
    EVP_CIPHER_CTX_free(ctx);
  </code></pre>