泄漏

2012-09-05 131 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