문제

I'm trying to fix "Potential leak of an object". I have warning on

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

and same on

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

Xcode saying: Call to function 'ABRecordCopyValue' returns a Core Foundation object with a +1 retain count Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1

I dont understand how fix it.

    -(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;
}
도움이 되었습니까?

해결책

You have to split

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

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

CFTypeRef values = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(values);
CFRelease(values);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top