我的应用程序使用相互身份验证连接到我的服务器,因此我有一个包含证书的.p12文件。一切都按照它应该的方式工作,但当我使用Instruments分析我的应用程序时,它检测到下面这行中的内存泄漏:
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]){
NSData* p12data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"]];
CFArrayRef itemsCFArray = nil;
NSDictionary* dico = [NSDictionary dictionaryWithObjectsAndKeys:@"password",kSecImportExportPassphrase, nil];
// MEMORY LEAK just below
OSStatus check = SecPKCS12Import((__bridge CFDataRef)p12data, (__bridge CFDictionaryRef)dico, &itemsCFArray);
if(check != noErr){
NSLog(@"Error importing PKCS");
}
NSArray* items = (__bridge NSArray*)itemsCFArray;
SecIdentityRef identityRef = (__bridge SecIdentityRef)[[items objectAtIndex:0] objectForKey:(__bridge id)kSecImportItemIdentity];
NSURLCredential* credential = [NSURLCredential credentialWithIdentity:identityRef certificates:nil persistence:NSURLCredentialPersistenceNone];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
}我尝试使用CFDictionaryRef代替,但它不能解决错误。
我找到了一个有同样问题的人,但他的解决方案是ios4,而我正在使用ios5 (实际上,我已经在做同样的事情了):http://www.ipup.fr/forum/viewtopic.php?id=2855 (法语,对不起)
我该如何解决这个问题?苹果会因为这个内存泄漏而拒绝我的应用吗?
发布于 2012-08-08 03:21:38
我不认为问题出在字典上,而是itemsCFArray似乎被泄露了。SecPKCS12Import将CF引用传递回itemsCFArray,当使用完其中的对象时,您将需要对其执行CFRelease操作。
尝试在创建凭据后调用CFRelease(itemsCFArray)。
https://stackoverflow.com/questions/11671000
复制相似问题