Later on in my code, I already release
policy
, so I don't believe that is the culprit.
Fair enough. I tried reproducing this with a small test project and it doesn’t leak for me. Pasted in below is the relevant code from my test project. Lines 39 through 42 are my attempts to reproduce what you’re doing with your code.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
@interface MainViewController () <NSURLSessionDelegate>
@property (nonatomic, strong, readwrite) NSURLSession * session;
@end
@implementation MainViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
#pragma unused(tableView)
#pragma unused(indexPath)
if (self.session == nil) {
NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
NSURL * url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[[self.session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
#pragma unused(data)
#pragma unused(response)
#pragma unused(error)
NSLog(@"task is done");
}] resume];
NSLog(@"task did start");
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
#pragma unused(session)
NSString * authenticationMethod = challenge.protectionSpace.authenticationMethod;
NSLog(@"challenge %@", authenticationMethod);
if ([authenticationMethod isEqual:NSURLAuthenticationMethodServerTrust]) {
SecTrustRef trust;
trust = challenge.protectionSpace.serverTrust;
SecPolicyRef policy = SecPolicyCreateBasicX509();
CFIndex certificateCount = SecTrustGetCertificateCount(trust);
NSLog(@"certificate count: %zd", certificateCount);
CFRelease(policy);
NSURLCredential * credential = [NSURLCredential credentialForTrust:trust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
} else {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
}
@end