简体   繁体   中英

What is causing this memory leak in iOS

I realize this question has been asked before, but I'm not sure how it applies in my scenario. Using instruments, I have discovered a memory leak, but I'm not sure how to go about correcting the leak. The leak occurs on the OSStatus status line.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM