3

我的项目中有以下代码

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

    [popContactsForm dismissPopoverAnimated:YES];

    [ssemailvc setName: (NSString *)ABRecordCopyValue(person,kABPersonFirstNameProperty)];        
    ABMultiValueRef emails = (NSString *)ABRecordCopyValue(person,kABPersonEmailProperty);


    if (ABMultiValueGetCount(emails)>0) {
        [ssemailvc setEmail:(NSString *)ABMultiValueCopyValueAtIndex(emails,0)];
    }else [ssemailvc setEmail:@""];

    CFRelease(emails);


    [peoplePicker dismissModalViewControllerAnimated:YES];      
    [popEmailForm setPopoverContentSize:CGSizeMake(350.0, 225.0)];

    return NO;
}

我收到以下警告

1.调用函数'ABRecordCopyValue'返回一个核心基础对象,保留计数为+1 2.对象泄漏:分配的对象在执行后期未引用,保留计数为+1

请告诉我如何克服这个问题

4

2 回答 2

6

如果使用 ARC,则可以使用桥接:

CFStringRef name = (CFStringRef)ABRecordCopyValue(person,kABPersonFirstNameProperty);
[ssemailvc setName:(__bridge NSString*)name];
CFRelease(name);

或过桥转移:

NSString * name = (__bridge_transfer NSString *)ABRecordCopyValue(person,kABPersonFirstNameProperty);
[ssemailvc setName:name];

如果您使用的是 MRC:

CFStringRef name = (CFStringRef)ABRecordCopyValue(person,kABPersonFirstNameProperty);
[ssemailvc setName:(NSString*)name];
CFRelease(name);
于 2013-05-21T10:50:45.750 回答
1

您看到警告的原因是ABRecordCopyValue返回 a CFStringRefwith +1 retain count

尝试

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

[popContactsForm dismissPopoverAnimated:YES];

CFStringRef strLname = ABRecordCopyValue(person, kABPersonLastNameProperty);
[ssemailvc setName: (NSString *) strLname];  

if(strLname){
CFRelease(strLname);
}

ABMultiValueRef emails = (NSString *)ABRecordCopyValue(person,kABPersonEmailProperty);


if (ABMultiValueGetCount(emails)>0) {
    [ssemailvc setEmail:(NSString *)ABMultiValueCopyValueAtIndex(emails,0)];
}else [ssemailvc setEmail:@""];

if(emails){
CFRelease(emails);
}

[peoplePicker dismissModalViewControllerAnimated:YES];      
[popEmailForm setPopoverContentSize:CGSizeMake(350.0, 225.0)];

return NO;
}
于 2013-05-21T10:54:07.860 回答