当我使用AddressBook
框架(Core Foundation)时,我遇到了问题,该框架使NSMutableDictionary
变量用于设置电话号码和地址簿中的其他字段(例如下面的电子邮件)发生内存崩溃。这种情况是第一次发生,当我调用此方法将数据传递到ABNewPerson
之后,当取消或保存联系人并返回我的视图控制器并单击另一次调用此方法时,NSMutabledictionary
可能会出现以下代码中的电话号码数据错误:
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;
}
这里是崩溃:
[CFString release]: message sent to deallocated instance