ABMultiValueCopyArrayOfAllValues 对象的潜在泄漏

Posted

技术标签:

【中文标题】ABMultiValueCopyArrayOfAllValues 对象的潜在泄漏【英文标题】:ABMultiValueCopyArrayOfAllValues Potential leak of an object 【发布时间】:2014-02-17 06:46:15 【问题描述】:

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

 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;

【问题讨论】:

【参考方案1】:

你必须分开

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

分成单独的命令,以便你可以释放ABRecordCopyValue()返回的对象:

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

【讨论】:

以上是关于ABMultiValueCopyArrayOfAllValues 对象的潜在泄漏的主要内容,如果未能解决你的问题,请参考以下文章