I have faced problem when I using AddressBook
framework (Core Foundation) which make memory crash for a NSMutableDictionary
variable using to set phone number and another fields in address book such as email below. This case happen as a first time when I call this method passing the data into ABNewPerson
after that when cancel or save contact and come back to my view controller and click another time to call this method give me error on phone number data in NSMutabledictionary
may code is below:
ABNewPersonViewController *newPersonViewController =[[ABNewPersonViewController alloc]init];
newPersonViewController.newPersonViewDelegate = self;
UINavigationController *newNavigationController = [[UINavigationController alloc]
initWithRootViewController:newPersonViewController];
newPersonViewController.displayedPerson =(ABRecordRef)[self buildContactDetails];
[self presentModalViewController:newNavigationController animated:YES];
- (ABRecordRef)buildContactDetails {
ABRecordRef person = ABPersonCreate();
CFErrorRef error = NULL;
// firstname
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge_retained CFTypeRef)(self.name.text), NULL);
// lastname
NSString *regionName = self.region.text;
ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge_retained CFTypeRef)(regionName), NULL);
//Phone
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
//Here is the problem this release dictionaryDetail after come back another time
NSString *phoneNumber = [[dictionaryDetail valueForKey:@"selectedRow"] valueForKey:@"phone" ];
NSLog(@"NNNNN %@",phoneNumber);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue,(__bridge CFTypeRef)(phoneNumber) , kABOtherLabel, NULL);
ABRecordCopyValue(person, kABPersonPhoneProperty);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
//CFRelease(phoneNumberMultiValue);
// Start of Address
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
NSString *subRegion = self.subRegion.text;
[addressDict setObject:@"" forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:@"" forKey:(NSString *)kABPersonAddressZIPKey];
[addressDict setObject:subRegion forKey:(NSString *)kABPersonAddressCityKey];
ABMultiValueAddValueAndLabel(address, (__bridge_retained CFTypeRef)(addressDict), (__bridge CFStringRef)(AMLocalizedString(@"Hospital", @"Hospital")), NULL);
ABRecordSetValue(person, kABPersonAddressProperty, address, &error);
//CFRelease(address);
// End of Address
if (error != NULL)
NSLog(@"Error: %@", error);
return person;
}
And here the crash :
[CFString release]: message sent to deallocated instance