Memory leak in iOS KeychainItemWrapper

I use the KeyChainItemWrapper from the Apple code example to store the user password for authentication, but when I call it to set the password:

   [keychain setObject:passwordField.text forKey:(id)kSecValueData];

It pumps memory leaks all over my shirt. The problem apparently traces back to line 274 in KeyChainItemWrapper.m, which is this:

if (SecItemCopyMatching((CFDictionaryRef)genericPasswordQuery, (CFTypeRef *)&attributes) == noErr)
{

How can I fix this, and should I be more careful when working with sample Apple code in the future?

Note. I can post more code, but I narrowed the problem down to this line using tools, and a complete sample code is easily accessible to any developer.

+5
source share
3 answers

KeyChainItemWrapper, , . [attributes release] writeToKeychain. . SecItemCopyMatching() , .

" , ..." , .

+5

resetKeychainItem, 191, KeyChainItemWrapper.m. , , , , .

:

- (void)resetKeychainItem
{
    ...
    // Default attributes for keychain item.
    [keychainItemData setObject:@"" forKey:(id)kSecAttrAccount]; // <-- Potential leak of an object
    [keychainItemData setObject:@"" forKey:(id)kSecAttrLabel];
    [keychainItemData setObject:@"" forKey:(id)kSecAttrDescription];

    // Default data for keychain item.
    [keychainItemData setObject:@"" forKey:(id)kSecValueData];
}

@ ". , " " , .

?

: , , .

:

if (!keychainItemData)
{
    self.keychainItemData = [[NSMutableDictionary alloc] init];
}

:

if (!keychainItemData)
{
    self.keychainItemData = [[[NSMutableDictionary alloc] init] autorelease];
}

.

+3

I found another leak in - (void) resetKeychainItem.

It should be

self.pKeychainItemData = [[[NSMutableDictionary alloc] init] autorelease];.

0
source

All Articles