访问电话簿ios时崩溃的应用程序

时间:2014-08-06 13:56:51

标签: ios objective-c ios7 crash-reports abaddressbook

ios memory leak when accessing phone book这是我的orignal问题,这是崩溃日志。内存泄漏问题已经解决,但是当我访问联系人时弹出应用程序崩溃并再次弹出导航控制器推送和访问联系人,重复此过程3-4次,应用程序将崩溃。

崩溃日志:

Aug  6 19:11:08 IPad  <Warning>: Fetching contact info ----> 

Aug  6 19:11:08 IPad  <Notice>: (Warn ) [ABLog]: <ABSource.c ABAddressBookCopyDefaultSource:111> Actual default source doesn't exist

Aug  6 19:11:57 IPad com.apple.debugserver-310.2[449] <Warning>: Got a 'k' packet, killing the inferior process.

Aug  6 19:11:57 IPad com.apple.debugserver-310.2[449] <Warning>: Sending ptrace PT_KILL to terminate inferior process.

Aug  6 19:11:57 IPad com.apple.debugserver-310.2[449] <Warning>: 38 +82.923904 sec [01c1/060b]: error: ::ptrace (request = PT_THUPDATE, pid = 0x01c2, tid = 0x2603, signal = 0) err = Resource busy (0x00000010)

Aug  6 19:11:57 IPad com.apple.debugserver-310.2[449] <Warning>: 39 +0.005248 sec [01c1/060b]: error: ::task_info ( target_task = 0x1c0b, flavor = TASK_BASIC_INFO, task_info_out => 0x27d93d48, task_info_outCnt => 8 ) err = (os/kern) invalid argument (0x00000004)

Aug  6 19:11:57 IPad com.apple.launchd[1] (UIKitApplication:com.baltech.gaurav[0x7f50][450]) <Notice>: (UIKitApplication:com.baltech.gaurav[0x7f50]) Exited: Killed: 9

Aug  6 19:11:57 IPad com.apple.debugserver-310.2[449] <Warning>: Waited 20 ms for process to be reaped (state = Exited)

Aug  6 19:11:57 IPad com.apple.debugserver-310.2[449] <Warning>: 40 +0.025414 sec [01c1/1207]: error: ::read ( -1, 0x4089ec, 18446744069414585344 ) => -1 err = Bad file descriptor (0x00000009)

Aug  6 19:11:57 IPad com.apple.debugserver-310.2[449] <Warning>: Exiting.

Aug  6 19:11:57 IPad backboardd[28] <Warning>: Application 'UIKitApplication:com.baltech.gaurav[0x7f50]' exited abnormally with signal 9: Killed: 9

这是我的更新代码

- (NSArray *)getAllContacts{

    CFErrorRef *error = nil;


    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

    __block BOOL accessGranted = NO;
    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    }
    else { // we're on iOS 5 or older
        accessGranted = YES;
    }

    if (accessGranted) {

#ifdef DEBUG
        NSLog(@"Fetching contact info ----> ");
#endif


        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];


        for (int i = 0; i < nPeople; i++)
        {
            ContactsData *contacts = [ContactsData new];

            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

            //get First Name and Last Name

            contacts.firstNames = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

            contacts.lastNames =  (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

            if (!contacts.firstNames) {
                contacts.firstNames = @"";
            }
            if (!contacts.lastNames) {
                contacts.lastNames = @"";
            }

            // get contacts picture, if pic doesn't exists, show standart one

            //            NSData  *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
            //            contacts.image = [UIImage imageWithData:imgData];
            //            if (!contacts.image) {
            //                contacts.image = [UIImage imageNamed:@"NOIMG.png"];
            //            }
            //            //get Phone Numbers
            //
            //            NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
            //
            //            ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            //            for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
            //
            //                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
            //                NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
            //                [phoneNumbers addObject:phoneNumber];
            //
            //                //NSLog(@"All numbers %@", phoneNumbers);
            //
            //            }


            //            [contacts setNumbers:phoneNumbers];

            //get Contact email

            NSMutableArray *contactEmails = [NSMutableArray new];
            ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

            for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
                CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
                NSString *contactEmail = (__bridge_transfer NSString *)contactEmailRef;

                [contactEmails addObject:contactEmail];
                // NSLog(@"All emails are:%@", contactEmails);


            }
            CFBridgingRelease(multiEmails);
            [contacts setEmails:contactEmails];





            //---- if no mail id found then dont add contact
            if (contactEmails && contactEmails.count != 0) {
                [items addObject:contacts];
            }


#ifdef DEBUG
            //            NSLog(@"Person is: %@ # %@", contacts.firstNames,contacts.lastNames);
            //            NSLog(@"Phones are: %@", contacts.numbers);
            //            NSLog(@"Email is:%@", contacts.emails);
#endif




        }
        CFBridgingRelease(addressBook);
        CFBridgingRelease(allPeople);
        CFBridgingRelease(source);

        return items;



    } else {
#ifdef DEBUG
        NSLog(@"Cannot fetch Contacts :( ");
#endif
        return nil;


    }

}

提前感谢:)

1 个答案:

答案 0 :(得分:1)

您使用了错误的数组计数值。尝试:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = CFArrayGetCount(allPeople);

顺便说一下,我之前看过这段代码时遇到同样的错误。你从哪里得到的?