调用 SSL_connect 时发生泄漏
我正在使用 OpenSSL,根据 Instruments 的说法,我在 SSL_connect 处开始出现内存泄漏。
SSL_CTX *ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
NSLog(@"Could not initialize ctx");
[self release];
return nil;
}
if(!SSL_CTX_use_certificate_chain_file(ctx, [PEMFile UTF8String])) {
NSLog(@"Can't read certificate file");
[self release];
return nil;
}
if(!(SSL_CTX_use_PrivateKey_file(ctx, [PEMFile UTF8String], SSL_FILETYPE_PEM))) {
NSLog(@"Can't read key file");
[self release];
return nil;
}
_sock = [self _tcpConnectWithHost:host port:port];
if (_sock < 0) {
[self release];
return nil;
}
_sslPointer = SSL_new(ctx);
BIO *bio = BIO_new_socket(sock, BIO_NOCLOSE);
SSL_set_bio(_sslPointer, bio, bio);
if(SSL_connect(_sslPointer) <= 0) {
NSLog(@"SSL connect error");
[self release];
return nil;
}
SSL_CTX_free(ctx);
然后我在调用 dealloc 时释放它。这就是泄漏出现的时候。
SSL_free(_sslPointer);
close(_sock);
泄漏开始的调用堆栈是:
0 libSystem.B.dylib malloc
1 libcrypto.0.9.8.dylib CRYPTO_malloc
2 libcrypto.0.9.8.dylib asn1_item_ex_combine_new
3 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
4 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
5 libcrypto.0.9.8.dylib asn1_template_ex_d2i
6 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
7 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
8 libcrypto.0.9.8.dylib asn1_template_ex_d2i
9 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
10 libcrypto.0.9.8.dylib x509_name_ex_d2i
11 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
12 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
13 libcrypto.0.9.8.dylib asn1_template_ex_d2i
14 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
15 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
16 libcrypto.0.9.8.dylib asn1_template_ex_d2i
17 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
18 libcrypto.0.9.8.dylib ASN1_item_d2i
19 libssl.0.9.8.dylib ssl3_get_server_certificate
20 libssl.0.9.8.dylib ssl3_connect
21 libssl.0.9.8.dylib ssl23_connect
I am using OpenSSL and according to Instruments, I have a memory leak started at SSL_connect.
SSL_CTX *ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
NSLog(@"Could not initialize ctx");
[self release];
return nil;
}
if(!SSL_CTX_use_certificate_chain_file(ctx, [PEMFile UTF8String])) {
NSLog(@"Can't read certificate file");
[self release];
return nil;
}
if(!(SSL_CTX_use_PrivateKey_file(ctx, [PEMFile UTF8String], SSL_FILETYPE_PEM))) {
NSLog(@"Can't read key file");
[self release];
return nil;
}
_sock = [self _tcpConnectWithHost:host port:port];
if (_sock < 0) {
[self release];
return nil;
}
_sslPointer = SSL_new(ctx);
BIO *bio = BIO_new_socket(sock, BIO_NOCLOSE);
SSL_set_bio(_sslPointer, bio, bio);
if(SSL_connect(_sslPointer) <= 0) {
NSLog(@"SSL connect error");
[self release];
return nil;
}
SSL_CTX_free(ctx);
I then release it when dealloc is called. This is when the leak appears.
SSL_free(_sslPointer);
close(_sock);
Call stack for where the leak starts is:
0 libSystem.B.dylib malloc
1 libcrypto.0.9.8.dylib CRYPTO_malloc
2 libcrypto.0.9.8.dylib asn1_item_ex_combine_new
3 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
4 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
5 libcrypto.0.9.8.dylib asn1_template_ex_d2i
6 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
7 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
8 libcrypto.0.9.8.dylib asn1_template_ex_d2i
9 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
10 libcrypto.0.9.8.dylib x509_name_ex_d2i
11 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
12 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
13 libcrypto.0.9.8.dylib asn1_template_ex_d2i
14 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
15 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
16 libcrypto.0.9.8.dylib asn1_template_ex_d2i
17 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
18 libcrypto.0.9.8.dylib ASN1_item_d2i
19 libssl.0.9.8.dylib ssl3_get_server_certificate
20 libssl.0.9.8.dylib ssl3_connect
21 libssl.0.9.8.dylib ssl23_connect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的最明显的事情是,只有在一切正常的情况下,您才可以释放
SSL_CTX *ctx
。但是,在创建 ctx 之前,您有很多可能的退出,但您从未free()
它。The most obvious thing I see is that you're only ever freeing the
SSL_CTX *ctx
if everything works out OK. But you have a whole bunch of possible exits before that wherectx
has been created, but you neverfree()
it.