我正在尝试获取联系人的电子邮件地址和电子邮件地址的类型(工作/家庭)。这是我写的代码
//Assume that 'personRef' of type ABRecordRef is available
....
ABMultiValueRef emailRef = ABRecordCopyValue(personRef, kABPersonEmailProperty);
NSMutableArray *emailAddresses = nil, *emailAddressLabels = nil;
int ctr = ABMultiValueGetCount(emailRef);
if(ctr!=0) {
emailAddresses = [[NSMutableArray alloc]init];
emailAddressLabels = [[NSMutableArray alloc]init];
for(int i=0; i<ctr; i++) {
NSString *eId = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(emailRef, i);
[emailAddresses addObject:eId];
CFStringRef label = ABMultiValueCopyLabelAtIndex (emailRef, i);
if(label!=NULL) {
NSString *eType = (__bridge_transfer NSString*)ABAddressBookCopyLocalizedLabel(label);
if([eType isEqualToString:@""]) {
[emailAddressLabels addObject:@"Email"];
} else {
[emailAddressLabels addObject:eType];
}
CFRelease(label);
}
}
}
代码在CFRelease(标签)崩溃,但为了防止内存泄漏,我应该这样做。当我尝试以下
NSString *eType = (__bridge_transfer NSString*) ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex (emailRef, i));
我从ARC收到以下警告
1. Call to function 'ABMultiValueCopyLabelAtIndex' returns a Core Foundation object with a +1 retain count
2. Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1
现在我的问题是,如何进行嵌套__bridge_transfer调用?
答案 0 :(得分:0)
NSString *eType = (__bridge_transfer NSString*)
ABAddressBookCopyLocalizedLabel(
ABMultiValueCopyLabelAtIndex (emailRef, i) /* <-- this object is leaked */
);
此代码无效,因为您在此处泄漏了标签(我认为这是您的观点?)。
你应该在Instruments NSZombie工具下运行该代码,它将跟踪所有保留/释放,你将知道发生了什么,因为坦率地说,看看代码,我不明白为什么它是错的。