title: Increases physical memory continuously Visual C++ CryptMsgClose and CryptReleaseContext
body: <p>I am using C++ and Visual Studio 2005.</p>
  
  <p>I have a project that memory Increases in a very abnormal. When debug the code I Realized That there are Several parts That Contribute to it. Like this for example:</p>
  
  <pre><code> // has to add crypt32.lib to link
  #include &lt;windows.h&gt;
  #define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  
  void memoryUP( const unsigned char *pData, int cData )
  {
      HCRYPTMSG  msg  = NULL;
      HCRYPTPROV hProv        = NULL;
  
      CryptAcquireContext(&amp;hProv,NULL,NULL,PROV_RSA_FULL,0);
  
      msg = CryptMsgOpenToDecode(MY_ENCODING_TYPE,0,0,hProv,NULL,NULL); 
  
      if(!(CryptMsgUpdate( msg, pData, cData, TRUE)))
      {
          if(msg != NULL)
          {
              CryptMsgClose(msg);
              msg = NULL;
          }
      }
  
      if (hProv != NULL)
          CryptReleaseContext(hProv,0);
  
      if (msg != NULL)
      {
          CryptMsgClose(msg);
          msg = NULL;
      }
  }
  
  int main(int argc, char** argv)
  {
      MyFile myfile = myReadFile("c:\\file.p7s");
  
      {
          for(int i=0; i&lt;100000; ++i)
          {
              memoryUP( myfile._data, myfile._length );
          }
      }
  
      delete myfile;
  
      return 0;
  }
  </code></pre>
  
  <p>When I run this code, the memory goes up continuously "when call CryptMsgUpdate". Am I deallocating wrong?</p>
  
  <p>I tried to use <a href="http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx" rel="nofollow">Memory Leak Detection Enabling</a> method to detect memory leak but nothing appears:</p>
  
  <pre><code>#define _CRTDBG_MAP_ALLOC
  #include &lt;stdlib.h&gt;
  #include &lt;crtdbg.h&gt;
  
  and
  
  _CrtDumpMemoryLeaks();
  </code></pre>
  
  <p>Thanks in Advance</p>