开发者

EXC_BAD_ACCESS when executing ABAddressBookSave !

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);

 NSMutableDictionar开发者_运维问答y *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);
 }


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜