title: Close SSL connection correctly with OpenSSL
body: <p>I have SSL connection in main thread and have memory leak. </p>
  
  <p>I close connection in this way (<code>_ssl — SSL*</code> object): </p>
  
  <pre><code>SSL_shutdown(_ssl);
  SSL_free (_ssl);
  </code></pre>
  
  <p>But memory isn't freed correctly — after every new client some memory (~20kb) doesn't freed. I made an experiment: make many connections(~7000) and close them after establishing. Graph shows that memory was really growing up during all time. It's very important for me that memory will be unchangeable from connection to connection. Any suggestions? </p>
  
  <p>Graph:
  <img src="https://i.stack.imgur.com/xJYkt.jpg" alt="enter image description here"></p>
  
  <p>UPD:
  All functions that I use:</p>
  
  <p>It's executed once when server is start (I think it can't reason for increasing memory from connection to connection):</p>
  
  <pre><code>    SSL_load_error_strings();
      SSLeay_add_ssl_algorithms();
      SSL_METHOD* meth = TLSv1_server_method();
  
      _ctx = SSL_CTX_new (meth);
  
      SSL_CTX_set_verify(_ctx, SSL_VERIFY_PEER, NULL);
      SSL_CTX_set_verify_depth(_ctx, 1);
  
      if (!_ctx) {
          ERR_print_errors_fp(stderr);
          exit(2);
      }
  
      if (SSL_CTX_load_verify_locations(_ctx, "keys/c1.crt", NULL) &lt;= 0)
      {
          ERR_print_errors_fp(stderr);
          exit(3);
      }
  
  
      SSL_CTX_set_verify(_ctx, SSL_VERIFY_PEER, NULL);
      SSL_CTX_set_verify_depth(_ctx,1);
  
      if (SSL_CTX_use_certificate_file(_ctx, CERTF, SSL_FILETYPE_PEM) &lt;= 0) {
          ERR_print_errors_fp(stderr);
          exit(3);
      }
      if (SSL_CTX_use_PrivateKey_file(_ctx, KEYF, SSL_FILETYPE_PEM) &lt;= 0) {
          ERR_print_errors_fp(stderr);
          exit(4);
      }
  
      if (!SSL_CTX_check_private_key(_ctx)) {
          fprintf(stderr, "Private key does not match the certificate public key\n");
          exit(5);
      }
  </code></pre>
  
  <p>It's executed for every connection in the start:</p>
  
  <pre><code>    ERR_remove_state(0);
  
      _ssl = SSL_new (ctx);
      if (_ssl == NULL)
      {
          _isValid = false;
          return;
      }
      SSL_set_fd (_ssl, _sd-&gt;get_num());
      int err = SSL_accept (_ssl);
      if (err == -1)
      {
          ERR_print_errors_fp(stderr);
          _isValid = false;
          return;
      }
  </code></pre>