0

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)peoplePickerNavigationController:(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

MrPink
  • 1,325
  • 4
  • 18
  • 27

1 Answers1

2

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);
}
Jilouc
  • 12,684
  • 4
  • 46
  • 43
  • Absolutely right, CFRelease is needed. Annoyingly, it looks like ABPeoplePickerNavigationController, itself, leaks. – Rob May 30 '12 at 01:21