从联系人处获取电话号码时CFStringRef可能发生泄漏

时间:2011-01-19 05:40:48

标签: iphone

我正在从地址簿中获取电话号码,因为我正在使用此代码。

- (IBAction)contacts {


    ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];
    peoplePickerController.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePickerController animated:NO];
    [peoplePickerController release];

}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person {


    return YES;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
                          identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonPhoneProperty) {
    ABMultiValueRef phonenumbers = ABRecordCopyValue(person, property);
    CFStringRef phonenumberselected = ABMultiValueCopyValueAtIndex(phonenumbers, identifier);

    NSString *aNSString = (NSString *)phonenumberselected;
    if ([share_toadd length] == 0) {
        [share_toadd appendString:aNSString];
    }
    else {
        [share_toadd appendString:@","];
        [share_toadd appendString:aNSString];
    }

    share_textfield.text = share_toadd;
    // Return to the main view controller.
    [ self dismissModalViewControllerAnimated:YES ];
    return NO;

    [share_textfield release];
}   
return YES;

}

但是我在CFStringRef

时遇到了潜在的泄漏
 Potential leak of an object allocated on line 1126

 Call to function 'ABRecordCopyValue' returns a Core Foundation object with a +1 retain count (owning reference)

 Object allocated on line 1126 is no longer referenced after this point and has a retain count of +1 (object leaked)

任何人都可以帮助我。

我该如何解决它。

1 个答案:

答案 0 :(得分:3)

完成对象使用后,您应该调用CFRelease(phonenumbersselected)。