2016-02-12 70 views
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; 
} 

回答