是什么导致iOS内存泄漏

时间:2016-02-12 19:05:08

标签: ios objective-c memory-leaks

我之前已经意识到这个问题,但我不确定它在我的场景中是如何应用的。使用仪器,我发现了内存泄漏,但我不确定如何纠正泄漏。泄漏发生在OSStatus status行。

// 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;
}

1 个答案:

答案 0 :(得分:4)

SecItemCopyMatching copies an attribute into attrResult

Since you have a copy, you are responsible for releasing it at some point with:

CFRelease(attrResult)

It's all described in the Core Foundation Memory Management Guide