是什么导致iOS内存泄漏

是什么导致iOS内存泄漏,ios,objective-c,memory-leaks,Ios,Objective C,Memory Leaks,我知道以前有人问过这个问题,但我不确定它在我的场景中是如何应用的。使用仪器,我发现了一个内存泄漏,但我不知道如何纠正这个泄漏。泄漏发生在OSStatus状态行 // Set up a query dictionary with the base query attributes: item type (generic), username, and service NSArray *keys = [[NSArray alloc] initWithObjects: (__bridge_transf

我知道以前有人问过这个问题,但我不确定它在我的场景中是如何应用的。使用仪器,我发现了一个内存泄漏,但我不知道如何纠正这个泄漏。泄漏发生在
OSStatus状态

// 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;
}
//使用基本查询属性设置查询字典:项类型(通用)、用户名和服务
NSArray*键=[[NSArray alloc]initWithObjects:(uu桥u传输NSString*)kSecClass,kSecAttrAccount,kSecAttrService,nil];
NSArray*对象=[[NSArray alloc]initWithObjects:(\uuu bridge\u transfer NSString*)kSecClassGenericPassword,username,serviceName,nil];
NSMutableDictionary*query=[[NSMutableDictionary alloc]initWithObjects:objects-forKeys:keys];
//首先查询属性,以防我们已经有一个没有密码数据集的Keychain项。
//这种不正确的项目可能产生的一种方式是由于之前的(不正确的)
//此代码的版本(将密码设置为通用属性而不是密码数据)。
NSMutableDictionary*attributeQuery=[query mutableCopy];
[attributeQuery setObject:(id)kCFBooleanTrue forKey:(_bridge_transfer id)kSecReturnAttributes];
CFTypeRef attrResult=NULL;

OSStatus status=SecItemCopyMatching((uu桥CFDictionaryRef)attributeQuery,(CFTypeRef*)和attrResult)
SecItemCopyMatching
将属性复制到
attrResult

由于您有一份副本,您有责任在某个时候发布它,包括:

CFRelease(attrResult)

这些都在

SecItemCopyMatching
中描述,它将属性复制到
attrResult

由于您有一份副本,您有责任在某个时候发布它,包括:

CFRelease(attrResult)
这一切都在报告中描述过