释放ABAddressBookRef时崩溃

时间:2014-03-30 10:18:51

标签: ios crash abaddressbook

我有这个列举地址簿联系人的代码 -

  +(void)enumerateAddressBookContacts:(void(^)(NSString *name, NSArray *phoneNumbers, NSArray *emailAddresses, NSData *imageData, NSString *recordId))enumerationBlock  failure:(void (^)( NSError *error))failure{

    [self requestAddressBookPermissionsWithCompletion:^{
    if (enumerationBlock)
    {
        CFErrorRef error = nil;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
        CFArrayRef addressBookPeople = ABAddressBookCopyArrayOfAllPeople (addressBook);
        NSArray* people = (__bridge NSArray*)addressBookPeople;

        if ([people count])
        {

            for (NSUInteger i = 0; i < [people count]; i++)
            {
                ABRecordRef person = (__bridge ABRecordRef)people[i];

                NSArray *phoneNumbers = [self phoneNumbersOfPerson:person];
                NSArray *emailAddresses = [self emailAddressesOfPerson:person];
                NSString *fullName = [self fullNameOfPerson:person];
                NSData *imageData = [self imageDataOfPerson:person];
                NSString *recordId = [NSString stringWithFormat:@"%d", ABRecordGetRecordID(person)];

                void (^block)(NSString*, NSArray *, NSArray *, NSData*, NSString*) = [enumerationBlock copy];
                block(fullName,phoneNumbers,emailAddresses,imageData,recordId);

            }
        }
        else
        {
            //No contacts in addressbook
            if (failure) {
                NSError *error = [NSError errorWithDomain:kAddressBook code:kAddressBookUploadFailReasonEmptyAddressBook userInfo:@{NSLocalizedDescriptionKey:kAddressBookUploadFailReason[kAddressBookUploadFailReasonEmptyAddressBook]}];
                failure(error);
            }
        }

          if (addressBookPeople)
         {
             CFRelease(addressBookPeople);
         }

         if (addressBook)
         {
             CFRelease (addressBook);
        }
    }

} failure:^(NSError *error) {
    if (failure) {
        failure(error);
    }   
}];
}

我在方法结束时释放ABAddressBookRef对象时遇到了崩溃。

这是一个枚举方法的例子(它们都是相同的) -

+ (NSArray*)phoneNumbersOfPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phones = ABRecordCopyValue (person, kABPersonPhoneProperty);
    NSMutableArray *phoneNumbers = [NSMutableArray array];
    CFIndex phonesNumberCount = ABMultiValueGetCount (phones);
    if (phonesNumberCount > 0)
    {
        for (CFIndex index = 0; index < phonesNumberCount; index++)
        {
            CFStringRef phoneValue = ABMultiValueCopyValueAtIndex (phones, index);
            [phoneNumbers addObject:(__bridge_transfer NSString *)phoneValue];

            if (phoneValue)
            {
                CFRelease (phoneValue);
            }
        }
    }

    if (phones)
    {
        CFRelease (phones);
    }

    return phoneNumbers;
}

当我删除释放时,我不会崩溃,但我认为我会有泄漏。 任何想法可能是什么原因?

由于

3 个答案:

答案 0 :(得分:0)

很好地发现了问题,我相信它可以帮助其他用户 -

看起来(__bridge_transfer NSString *)已经减少CFStringRef的保留计数, 所以没有必要发布它。

__bridge

似乎不是这样

答案 1 :(得分:0)

我还建议检查这两个函数的结果:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,&amp; error);   CFArrayRef addressBookPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

这两个中的任何一个都可以返回NULL,并且尝试访问它将导致崩溃。

答案 2 :(得分:0)

您没有显示您的函数 requestAddressBookPermissionsWithCompletion 的功能。我将在我的地址簿管理器中粘贴部分代码:

//Saving current thread queue.
dispatch_queue_t currentQueue = dispatch_get_global_queue(0,0);
CFErrorRef error = nil;
addressBook = ABAddressBookCreateWithOptions(NULL,&error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    // callback can occur in background, address book must be accessed on thread it was created on
    dispatch_async(currentQueue, ^{
        if(error || !granted){

            //SOME ERROR HAPPENED OR USER DIDN'T ALLOW ACCESSING CONTACTS -> SHOW SOME FEEDBACK
        }
        else {
            // DO WHATEVER YOU NEED WITH THE ADDRESSBOOK

        }
        CFRelease(addressBook);
    });
});

我可以毫无问题地发布地址簿