内存泄漏ABAdressbook

时间:2012-02-23 11:52:20

标签: iphone ios abaddressbook

我在以下方法中遇到内存泄漏:

- (void) SyncContactData
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

    for( int i = 0 ; i < nPeople ; i++ )
    {
        //dicContact = [[NSMutableDictionary alloc] init];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i );
        NSString *str = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        NSString *strSub = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        int ii = [[NSString stringWithFormat:@"%@",strSub] length];
        if(str != nil || ii == 0)
            [arrNames addObject:strSub];
        else
            [arrNames addObject:@""];

        CFTypeRef multival = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        NSArray *arrayPh = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival);
        if([arrayPh count] > 0)
            [arrPhone addObject:[arrayPh objectAtIndex:0]];
        else
            [arrPhone addObject:@""];

        CFRelease(multival);

    }

    CFRelease(addressBook);
    CFRelease(allPeople);

}

在这里泄漏:

  1. NSString *str = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
  2. NSString *strSub = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
  3. NSArray *arrayPh = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival);

2 个答案:

答案 0 :(得分:0)

完成后,您需要释放最初复制的对象:

[str release];
[strSub release];
[arrayPh release];

如果启用了ARC,则可能需要使用CFRelease代替(并进行适当的转换)。

答案 1 :(得分:0)

那么,为什么不释放它们?

CFRelease(str);
CFRelease(strSub);
CFRelease(arrayPh);