- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonPhoneProperty) {
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhonePrope开发者_JS百科rty);
for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
CFRelease(multiPhones);
NSString *phoneNumber = (NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
RecipientContact *recipient = [[RecipientContact alloc] init];
recipient.phoneNumber = [NSString stringWithFormat:@"%@", phoneNumber];
recipient.name = nil;
[recipients addObject:recipient];
[recipient release];
[phoneNumber release];
}
}
}
[self dismissModalViewControllerAnimated:YES];
[self _addRecipients];
return NO;
}
- (void)_addRecipients {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *names = [[NSString alloc] init];
for (RecipientContact *recipient in recipients) {
names = [names stringByAppendingString:recipient.phoneNumber];
names = [names stringByAppendingString:@";"];
}
contactsField.text = names;
[pool release];
}
I'm trying to use this code in my project ,but it'll crash when I go into the addressbook twice.I found when I CFRelease one of multiPhones and phoneNumberRef,it go well.But when I CFRelease both of them,the app will crash.
I have seen the Core Foundation Naming Conventions,it says "if a function name contains the word "Create" or "Copy", you own the object.",but why I release both of them my app crashed,thank you.The problem is you are using toll free bridging and then releasing the NSString
NSString *phoneNumber = (NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
...
[phoneNumber release]; //<-- this is like calling CFRelease(phoneNumberRef); again
Either call [phoneNumber retain]
before CFRelease(phoneNumberRef);
or just dont CFRelease
the phoneNumberRef
.
精彩评论