iPhone ABPeoplePickerNavigationController Memory Leaks
I am going through my application killing off all the memory leaks and the analyze tool comes up with a leak using the abpeoplepickernavigationcontroller.
I understand it is something to do with a copy method? but don't know how to go about releasing it where and at the write time.
I basically need to present the modal view, select the phonenumber then drag it back into the textfield. heres my code
-(IBAction)openAddressBook{
ABPeoplePickerNavigationController *peoplepicker = [[ABPeoplePickerNavigationController alloc] init];
peoplepicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplepicker animated:YES];
[peoplepicker release];
}
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavi开发者_如何学CgationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
//Retrieving the phone number property of ABRecordRef
ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
phone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *brokenNumber = [phone componentsSeparatedByString:@" "];
phone = [brokenNumber componentsJoinedByString:@""];
if(![phonenumber.text isEqualToString:@""])
phonenumber.text = [NSString stringWithFormat:@"%@%@", phonenumber.text, @";"];
phonenumber.text = [NSString stringWithFormat:@"%@%@", phonenumber.text, phone];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Thanks
The problem is likely to be here:
ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
phone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
You get objects copies with ABRecordCopyValue
and ABMultiValueCopyValueAtIndex
and they aren't released.
ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
if (phoneProperty) {
CFRelease(phoneProperty);
}
NSString *trimmedPhone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (phone) {
CFRelease(phone);
}
精彩评论