2

I have a view in my app that shows a form (email, first name, last name, date of birth) which loads into the fields, the corresponding data from the keychain allowing the user to change it etc. This works fine when there are already details stored in the keychain, but if there is nothing (when the app is run for the first time) it crashes when I go to release the keychain. In order to save a memory leak, I want to see if there is a better way of doing things. Im using apple's KeychainItemWrapper and my project does not use ARC.

Here is my code

// add data from keychain to fields if set
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"test" accessGroup:nil];
NSString *error;

NSData *dictionaryRep = [keychainItem objectForKey:kSecAttrService];
NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:dictionaryRep mutabilityOption:NSPropertyListImmutable format:nil errorDescription:&error];

if (error) {
   NSLog(@"An error occurred - %@", error);
    }
else{
    //successful so populate fields
    email_.text = [dictionary objectForKey:@"email"];
    firstName_.text = [dictionary objectForKey:@"firstname"];
    lastName_.text = [dictionary objectForKey:@"lastname"];
    dob_.text = [dictionary objectForKey:@"dob"];
}
[keychainItem release]; // crashes here with an EXC_BAD_ACCESS
skeg0
  • 187
  • 1
  • 1
  • 10
  • may be object of KeychainItemWrapper `keychainItem` using again after releaseing that's why you got EXC_BAD_ACCESS just remove release line code. – Nitin Gohel Jul 09 '13 at 10:12
  • Its not being used again - its the end of the method right after my code block above, removing the release and running "analyse" gives me a potential leak warning from Xcode. thanks! – skeg0 Jul 09 '13 at 10:19
  • Have you tried autorelease? – TheDarkKnight Jul 09 '13 at 10:59
  • With autorelease I get - 'malloc: *** error for object 0x1fd83c90: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug' – skeg0 Jul 09 '13 at 15:27

0 Answers0