ABMultiValueCopyArrayOfAllValues ​​Potential Object Leakage

I am trying to fix a "Potential Object Leak". I have a warning about

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

and same thing on

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

Xcode says: Calling the "ABRecordCopyValue" function returns a Core Foundation object with a save of +1. Object leaked: the selected object is not mentioned later in this execution path and has a save value of +1

I don’t understand how to fix this.

    -(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
source share
1 answer

You need to split

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

into separate commands so that you can free the object returned ABRecordCopyValue():

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

Source: https://habr.com/ru/post/1527271/


All Articles