[英]How do I free an SSL Connection from OpenSSL?
以下代碼是否正確釋放了所有分配的內存?
void foo(){
//set up connection
SSL *ssl = NULL;
SSL_METHOD *method = (SSL_METHOD *)SSLv23_client_method();
SSL_CTX *ctx = SSL_CTX_new(method);
BIO *bio = BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio, &ssl);
BIO_set_conn_hostname(bio, "facebook.com:443");
doConnect(ssl, ctx, bio);
...
doFree(ssl, ctx, bio);
}
void doConnect(SSL *ssl, SSL_CTX *ctx, BIO *bio){
BIO_reset(bio); //this is here in case we are trying to reconnect
if (BIO_do_connect(connection->bio) <= 0){
while ( BIO_should_retry(connection->bio)){
if (BIO_do_connect(connection->bio) > 0){
break;
}
}
//error handeling in case BIO_should_retry returns false omitted.
}
if (SSL_get_verify_result(connection->ssl) != X509_V_OK){
// Handle the failed verification
}
int socket = BIO_get_fd(bio, NULL);
}
void doFree(SSL *ssl, SSL_CTX *ctx, BIO *bio){
BIO_free_all(bio); //is this right?
}
我想知道這是否是釋放內存的正確方法的原因是,因為我當前正在獲取以下堆棧跟蹤信息,而且我不知道是否釋放內存不當,或者是否是其他某種錯誤(valgrind不報告任何內存錯誤,它只是在此處暫停)。
(gdb) bt #0 0x040010c2 in ?? () from /lib/ld-linux.so.2 #1 0x06a13a0b in write () at ../sysdeps/unix/syscall-template.S:82 #2 0x04153ae9 in ?? () from /lib/i386-linux-gnu/libcrypto.so.1.0.0 #3 0x041508e4 in BIO_write () from /lib/i386-linux-gnu/libcrypto.so.1.0.0 #4 0x040771f1 in ?? () from /lib/i386-linux-gnu/libssl.so.1.0.0 #5 0x040775ff in ?? () from /lib/i386-linux-gnu/libssl.so.1.0.0 #6 0x04078d2f in ?? () from /lib/i386-linux-gnu/libssl.so.1.0.0 #7 0x04077a64 in ?? () from /lib/i386-linux-gnu/libssl.so.1.0.0 #8 0x04074bde in ?? () from /lib/i386-linux-gnu/libssl.so.1.0.0 #9 0x0408eed1 in SSL_shutdown () from /lib/i386-linux-gnu/libssl.so.1.0.0 #10 0x0409b175 in ?? () from /lib/i386-linux-gnu/libssl.so.1.0.0 #11 0x04150638 in BIO_free () from /lib/i386-linux-gnu/libcrypto.so.1.0.0 #12 0x041511c4 in BIO_free_all () from /lib/i386-linux-gnu/libcrypto.so.1.0.0
可能是因為您的代碼未調用SSL_library_init();
。 另外,包括, main
和刪除對connection
引用使它對我來說都可以工作。
沒有SSL_library_init();
由於bio
為NULL
它在BIO_should_retry
崩潰。
我正在正確釋放內存。 原來,這個問題是我將OpenSSL與線程一起使用但未初始化OpenSSL線程系統的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.