存储在“名称”中的对象的潜在泄漏

时间:2017-03-08 10:19:51

标签: ios objective-c contacts

这是我的代码:

[ NSUInteger peopleCounter = 0; //set a initial value as ZERO.

//Create custom Contacts list
for (peopleCounter = 0; peopleCounter < [arrayOfAllPeople count]; peopleCounter++) {

    ABRecordRef thisPerson = (__bridge ABRecordRef) [arrayOfAllPeople objectAtIndex:peopleCounter]; // get every person record one by one.

    NSString *name = (__bridge NSString *) ABRecordCopyCompositeName(thisPerson); //get a person name

    ABMultiValueRef number = ABRecordCopyValue(thisPerson, kABPersonPhoneProperty); //get a person number

    //optimize a phone numbers
    for (NSUInteger emailCounter = 0; emailCounter < ABMultiValueGetCount(number); emailCounter++) {

        NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(number, emailCounter);
        ][1]

1 个答案:

答案 0 :(得分:0)

当您使用名称为ABRecordCopyCompositeName copy 的方法返回Core Foundation对象(如CFStringRef时)时,您应该考虑内存管理,因为ARC不起作用,除非你告诉它应该如何处理情况。

在您的情况下,您应该使用__bridge_transfer代替__bridge

NSString *name = (__bridge_transfer NSString *) ABRecordCopyCompositeName(thisPerson); //get a person name

因此ABRecordCopyCompositeName结果生命周期将与name变量生命周期相关联。

在您的示例中也注意ABMultiValueCopyValueAtIndex