1
我意識到這個問題之前已經問過,但我不知道它是如何適用於我的情況。使用儀器,我發現了內存泄漏,但我不知道如何去糾正泄漏。泄漏發生在OSStatus status
行。什麼導致這種內存泄漏在iOS
// Set up a query dictionary with the base query attributes: item type (generic), username, and service
NSArray *keys = [[NSArray alloc] initWithObjects: (__bridge_transfer NSString *) kSecClass, kSecAttrAccount, kSecAttrService, nil];
NSArray *objects = [[NSArray alloc] initWithObjects: (__bridge_transfer NSString *) kSecClassGenericPassword, username, serviceName, nil];
NSMutableDictionary *query = [[NSMutableDictionary alloc] initWithObjects: objects forKeys: keys];
// First do a query for attributes, in case we already have a Keychain item with no password data set.
// One likely way such an incorrect item could have come about is due to the previous (incorrect)
// version of this code (which set the password as a generic attribute instead of password data).
NSMutableDictionary *attributeQuery = [query mutableCopy];
[attributeQuery setObject: (id) kCFBooleanTrue forKey:(__bridge_transfer id) kSecReturnAttributes];
CFTypeRef attrResult = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) attributeQuery, (CFTypeRef *) &attrResult); <--- Here is the leak
NSLog(@"SFHFKeychainUtils => getPasswordForUsername => status => %d\n", status);
NSLog(@"SFHFKeychainUtils => getPasswordForUsername => attrResult => %@\n", attrResult);
NSLog(@"SFHFKeychainUtils => getPasswordForUsername => attributeQuery => %@\n", attributeQuery);
if (status != noErr) {
// No existing item found--simply return nil for the password
if (error != nil && status != errSecItemNotFound) {
//Only return an error if a real exception happened--not simply for "not found."
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];
}
return nil;
}