My app connects to my server using mutual authentification, so I have a .p12 file containing a certificate. Everything works the way it is supposed to, but when I’m profiling my app using Instruments, it detects a memory leak on this line :
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];
}
I’ve tried to use a CFDictionaryRef instead, but it does not resolve the error.
I found someone with the same problem, but his solution is ios4, and i’m using ios5 (Actually, I’m already doing the same thing) : http://www.ipup.fr/forum/viewtopic.php?id=2855 (in french, sorry)
How can I resolve this ? Will Apple reject my app because of this memory leak ?
I don’t think the issue is with the dictionary, itemsCFArray is what appears to be getting leaked. SecPKCS12Import passes back a CF reference to itemsCFArray, which you will need to CFRelease when you are done using the objects in it.
Try calling CFRelease(itemsCFArray) after creating the credential.