【问题标题】:ABMultiValueCopyArrayOfAllValues Potential leak of an objectABMultiValueCopyArrayOfAllValues 对象的潜在泄漏
【发布时间】: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;
}

【问题讨论】:

    标签: objective-c ios7 memory-leaks xcode5 abrecordcopyvalue


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多