调用函数'ABRecordCopyValue'返回一个带有+1保留计数的corefoundation对象

时间:2013-05-21 10:40:18

标签: ios objective-c abaddressbook

我的项目中有以下代码

- (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.call to function'ABRecordCopyValue'返回一个带有+1保留计数的corefoundation对象   2.object泄漏:在执行的后期没有引用已分配的对象,并且保留计数为+1

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

您看到警告的原因是ABRecordCopyValue返回CFStringRef +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;
}