泄漏

2012-09-05 107 views
0

这里钥匙扣接入代码泄漏: enter image description here泄漏

这里是我的代码:

+(NSString *)getSecureValueForKey:(NSString *)key { 

    // Retrieve a value from the keychain 
    NSDictionary *result; 
    NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease]; 
    NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease]; 
    NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys]; 

    // Check if the value was found 
    OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &result); 
    [query release]; 
    if (status != noErr) { 
     // Value not found 
     return nil; 
    } else { 
     // Value was found so return it 
     NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric]; 
     return value; 
     [result release]; 
    } 
} 

+(BOOL)storeSecureValue:(NSString *)value forKey:(NSString *)key { 
    // Get the existing value for the key 
    NSString *existingValue = [self getSecureValueForKey:key]; 

    // Check if a value already exists for this key 
    OSStatus status; 
    if (existingValue) { 
     // Value already exists, so update it 
     NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease]; 
     NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease]; 
     NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease]; 
     status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]); 
    } else { 
     // Value does not exist, so add it 
     NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease]; 
     NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease]; 
     NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease]; 
     status = SecItemAdd((CFDictionaryRef) query, NULL); 
    } 

    // Check if the value was stored 
    if (status != noErr) { 
     // Value was not stored 
     return false; 
    } else { 
     // Value was stored 
     return true; 
    } 
} 

你能帮我解决这个问题?每次我访问或存储钥匙串中的数据时都会发生泄漏。 我没有编程没有ARC很多时间,我只是不能跟踪这个泄漏!

谢谢!

+0

在返回值之前执行'[result release];' – Adam

回答

0

发布keysobjects创建query后,释放result返回value之前,实际上释放你的店安全值法过程中创建新的对象。

在这一点上,您的选择是,了解内存管理实际上是如何工作的,或者只需打开ARC并让它为您处理大部分内容。

+0

'keys'和'objects'是自动释放的。我宁愿手动释放它们,但仍然不泄漏。 – Adam