4

我正在尝试修复“对象的潜在泄漏”。我有警告

 NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(ABRecordCopyValue(person, kABPersonPhoneProperty));

同样在

NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(ABRecordCopyValue(person, kABPersonPhoneProperty));

Xcode 说:调用函数 'ABRecordCopyValue' 返回一个具有 +1 保留计数的 Core Foundation 对象对象泄漏:在此执行路径中稍后未引用分配的对象,并且保留计数为 +1

我不明白如何修复它。

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

    NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    NSString *fullName = @"";

    if (firstName != nil) {
        fullName = [fullName stringByAppendingString:firstName];
    }
    if (lastName != nil) {
        fullName =  [NSString stringWithString:[fullName stringByAppendingString:@" "]];
        fullName =  [NSString stringWithString:[fullName stringByAppendingString:lastName]];
    }

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:fullName];

    [contactsArray addObject:tempArray];
    _userNameTextField.text = [tempArray objectAtIndex:0];

    CFRelease((__bridge CFTypeRef)(firstName));
    CFRelease((__bridge CFTypeRef)(lastName));

    NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(ABRecordCopyValue(person, kABPersonPhoneProperty));

    NSArray *emails = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(ABRecordCopyValue(person, kABPersonEmailProperty));

    if (phones) {
        [tempArray addObject:[phones objectAtIndex:0]];
    }
    else{
        [tempArray addObject:@"No phone number was set."];
    }
    if (emails) {
        [tempArray addObject:[emails objectAtIndex:0]];
    }
    else{
        [tempArray addObject:@"No e-mail was set."];
    }

    // Now add the tempArray into the contactsArray.
    _mobPhoneTextField.text = [tempArray objectAtIndex:1];
    _emailTextField.text = [tempArray objectAtIndex:2];

    [[contacts presentingViewController] dismissViewControllerAnimated:YES completion:nil];

    return NO;
}
4

1 回答 1

4

你必须分开

NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(ABRecordCopyValue(person, kABPersonPhoneProperty));

进入单独的命令,以便您可以释放由返回的对象ABRecordCopyValue()

CFTypeRef values = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(values);
CFRelease(values);
于 2014-02-17T07:39:07.163 回答