我正在运行一个处理超过 500 个连接的 DTLS 服务器,每次关闭一些连接时,我都会看到内存利用率增加。
我想知道我下面的
Initialize_Sever_Context()
、create_connexion()
和 close_connexion()
方法是否存在内存泄漏。确切的代码太大,无法创建实际场景,所以我只是概述了该步骤。
如果需要任何额外信息,请告诉我?
我在 Linux 上使用 OpenSSL 版本 1.1.1k。
//connect_info structure user defined
{
void* sll;
void* bio;
....
}array_of_connections
*connect_info = &array_of_connections;
// global
SSL_CTX* server_ctx;
Initialize_Sever_Context()
{
// server_ctx is global
server_ctx = SSL_CTX_new(DTLS_server_method());
X509_VERIFY_PARAM *local_vpm = X509_VERIFY_PARAM_new()
//setting verify flags, cookie flags and cypher lists etc..
//....
SSL_CTX_set1_param(server_ctx, local_vpm);
}
create_connexion(connect_info)
{
// server_ctx is global
ssl = SSL_new(server_ctx);
bio = BIO_new_dgram(handler, BIO_NOCLOSE);
..
..
SSL_set_bio(ssl, bio, bio);
connect_info->ssl = ssl;
connect_info->bio = bio;
}
handle_closed_connexions()
{
for(conn = 1; conn<MAX_CONN;conn++)
{
close_connexion(connect_info[conn]);
}
}
close_connexion(connect_info)
{
// store prev ssl objects
SLL *local_ssl = connect_info -> ssl;
// make setup ready for the next connexions
// and start listening
create_connexion(connect_info)
// free the previous closed connections
SSL_free(local_ssl);
}
在
SSL_free()
里面,我们有 BIO_free_all(s->rbio)
、BIO_free_all(s->rbio)
和 BIO_CTX_free(s->ctx)
,最后是 OPENSSL_free(s)
。
据我了解,当我们这样做时,SSL对象内的所有成员(指针)都会被释放。 但在 OpenSSL 内部,在
SSL_free()
之后,没有任何指针被设置为 NULL
,所以我预计应用程序会崩溃。但是即使在指针被释放后我的应用程序仍然可以工作。
为什么 OpenSSL 在释放指针后不将指针设置为
free()
或者我可以假设我的应用程序使用上述方法是安全的吗?
我已查看帖子 openssl中的内存泄漏和 如何正确取消初始化openssl和其他,但他们都不满足我的要求,所以我问一个新问题。
没有任何意义。如果您确实需要将它们设置为99.99% 的情况下,指针不会被再次访问,因此将它们设置为
NULL
NULL
,那么您可以将它们设置为
NULL
。