I recently migrated a huge library to ARC, and a tool-free section is giving a headache. Here is the code:
+ (NSString *)getKeychainItem:(NSString *)identifier
{
NSString *fullIdentifier = [NSString stringWithFormat:@"%@%@", kIdentifierPrefix, identifier];
NSMutableDictionary *queryKeychain;
OSStatus status = noErr;
queryKeychain = [NSMutableDictionary dictionary];
// Set the public key query dictionary.
[queryKeychain setObject:(__bridge id)kSecClassGenericPassword
forKey:(__bridge id)kSecClass];
// Get the key.
CFDataRef data;
CFDictionaryRef queryKeychainCF = (__bridge CFDictionaryRef)queryKeychain;
status = SecItemCopyMatching(queryKeychainCF, (CFTypeRef *)&data);
NSData *passwordData = (__bridge_transfer NSData *)data;
NSString *password;
if (status == noErr)
{
password = [[NSString alloc] initWithBytes:[passwordData bytes]
length:[passwordData length]
encoding:NSUTF8StringEncoding];
}
else if (status != errSecItemNotFound)
{
NSLog(@"Error getting keychain item %@ -- OSStatus: %lu", identifier, status);
}
return password;
}
This should be pretty straight forward, however, the passwordData object is being overreleased and I have no idea why, the stack trace is this. If I just set passwordData
to nil
and don't do the __bridge__transfer
, it doesn't crash. Any Idea on why?
Thanks a lot!