2

I'm trying to create a new contact and add it to the AddressBook but when I get to the ABAddressSave line of code I get EXC_BAD_ACCESS. I cannot see what am I doing wrong, I enabled NSZombie to check if this is a memory related error but it didn't spot any. Can anybody tell me what is wrong with this code? Thank you in advance!

    CFErrorRef error = NULL;

 ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();

 ABRecordRef newRecord = ABPersonCreate();

 ABRecordSetValue(newRecord, kABPersonFirstNameProperty, @"Xxxxxx", &error);

 ABRecordSetValue(newRecord, kABPersonURLProperty,  @"Yyyyyy", &error);


 //Add phone numbers to record

 ABMutableMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
 ABMultiValueAddValueAndLabel(phones, @"1-555-555-5555", kABWorkLabel, NULL);


 ABRecordSetValue(newRecord, kABPersonPhoneProperty, phones, &error);

 CFRelease(phones);

 //Add email address to record

 ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
 ABMultiValueAddValueAndLabel(emails, @"xxx_xxx@yahoo.com", kABWorkLabel, NULL);


 ABRecordSetValue(newRecord, kABPersonEmailProperty, emails, &error);

 CFRelease(emails);

 ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

 NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];

 [addressDict setObject:@"xxx1" forKey:(NSString *)kABPersonAddressStreetKey];
 [addressDict setObject:@"xxx2" forKey:(NSString *)kABPersonAddressCityKey];
 [addressDict setObject:@"xxx3" forKey:(NSString *)kABPersonAddressStateKey];
 [addressDict setObject:@"xxx4" forKey:(NSString *)kABPersonAddressZIPKey];

 ABMultiValueAddValueAndLabel(multiAddress, addressDict,  kABWorkLabel, NULL);


 ABRecordSetValue(newRecord, kABPersonAddressProperty, multiAddress, &error);

 CFRelease(multiAddress);
 [addressDict release];


 ABAddressBookAddRecord(iPhoneAddressBook, newRecord, &error);
 ABAddressBookSave(iPhoneAddressBook, NULL);

 if(error != nil){
  NSLog(@"Error creating contact:%@", error);
 }
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Horatiu Paraschiv
  • 1,750
  • 2
  • 15
  • 37
  • note that you're doing the addRecord, not checking the error and then doing the save. I would check my error far more frequently. – KevinDTimm Mar 22 '10 at 19:27

2 Answers2

1

I would suggest running your code in Instruments with the Memory->Object Allocations template. It should very quickly show you which object is the problem and what memory deallocation is causing the problem.

rcw3
  • 3,034
  • 1
  • 27
  • 24
1

Ok I've figured it out, it wasn't a memory issue, actually the error is not even in the posted code because when I posted the code I cleaned it a little bit and the error it is not there anymore. Kinda stupid but I did It. So the error: when I was assigning an URL value I was assigning it with a simple ABRecordSetValue call and I should've used an ABMutableMultiValueRef. Also, everytime I was filling the record with a nil value I got a crash, so I think nil values aren't allowed when you build your Person object. Thanks you for your time!

Horatiu Paraschiv
  • 1,750
  • 2
  • 15
  • 37
  • i din't get: when I was assigning an URL value I was assigning it with a simple ABRecordSetValue call and I should've used an ABMutableMultiValueRef. what all values should not be set with abrecordSetValue? – Nikita P Dec 03 '12 at 09:37
  • @Horatiu if nil value is the case, then you missed something because before adding contact I have print First name, Last name and phone no in console. And each and every time it got filled but still I got the same crash "EXC_BAD_ACCESS". – Tapas Pal Apr 25 '13 at 10:19
  • Also we have to add ABAddressBookAddRecord(self.addressBook, record, nil) before ABAddressBookSave(self.addressBook, nil). – Pooja M. Bohora Oct 28 '15 at 12:36