我正在尝试创建一个新的联系人并将其添加到AddressBook但是当我到达ABAddressSave代码行时,我得到了EXC_BAD_ACCESS。我看不出我做错了什么,我启用NSZombie来检查这是否是与内存相关的错误,但它没有发现任何错误。谁能告诉我这段代码有什么问题?提前谢谢!
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);
}
答案 0 :(得分:1)
我建议使用Memory-> Object Allocations模板在Instruments中运行代码。它应该很快向您显示哪个对象是问题以及导致问题的内存释放。
答案 1 :(得分:1)
好的我已经弄清楚了,这不是内存问题,实际上错误甚至不在发布的代码中,因为当我发布代码时我清理了一点而且错误它不再存在了。有点傻但我做到了。所以错误:当我分配一个URL值时,我给它分配了一个简单的ABRecordSetValue调用,我应该使用ABMutableMultiValueRef。此外,每当我用nil值填充记录时,我遇到了崩溃,所以我认为在构建Person对象时不允许使用nil值。谢谢你的时间!