ABAdressbook 内存泄漏

发布于 2025-01-08 14:03:00 字数 1507 浏览 2 评论 0原文

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

- (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);

I am getting memory leaks in the following method :

- (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);

}

Getting leaks here:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2025-01-15 14:03:00

当您使用完最初复制的对象时,您需要释放它们:

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

如果启用了 ARC,您可能需要使用CFRelease(并适当地转换)。

You need to release objects that are originally copied when you are done with them:

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

If ARC is enabled, you might need to use CFRelease instead (and cast appropriately).

万劫不复 2025-01-15 14:03:00

那么,为什么不释放他们呢?

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

So, why don't released them?

CFRelease(str);
CFRelease(strSub);
CFRelease(arrayPh);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文