以下代码是否正确释放了所有已分配的内存?
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
答案 0 :(得分:0)
可能是因为您的代码没有调用SSL_library_init();
。添加,包含,main
和删除对connection
的引用使得它对我有用。
由于SSL_library_init();
BIO_should_retry
bio
已NULL
,因此{{1}}已崩溃{/ 1}}。
答案 1 :(得分:0)
我正确地释放了记忆。原来,问题是我使用OpenSSL和线程但没有初始化OpenSSLs线程系统的结果。