Sam B
Sam B

Reputation: 27608

ABAddressbook crash CFStringRef

I have an app which gets users address book first and last name. I have tested in both Simulator and on my own iPhone 4S running iOS 7 with XCode 5 and it works and runs fine.

Lately some international users have been complaining that my app keeps crashing on them. Fortunately I was able to download the crash log from iTunes, and when I symbolicate it I found the app crashed on at least one users at the following step. I asked the user what's so special about their addressbook and he mentioned that he has some names in English, Hebrew. My question is why would it crash at that line? Is it that CFStringRef is a sensative variable and I should use something else instead? I cannot for the life of me figure it out.

CRASH LOG:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x000000005152dca8
Triggered by Thread:  0

Thread 0 Crashed:
0   AppSupport 0x3237642d CPRecordGetProperty + 21
1   AppSupport 0x323765c1 CPRecordCopyProperty + 9
2   AddressBook 0x2e154457 ABRecordCopyValueUnfiltered + 79
3   AddressBook 0x2e1542f7 ABRecordCopyValue + 79
4   Contacts HD 0x0008a583 -[v1AddressBookTblController getValsForTable] (v1AddressBookTblController.m:147)

CODE:

- (IBAction) getValsForTable
{

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeopleArray;


    allPeopleArray = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);


    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

    //NSLog(@"Start LOOP");
    for (int i=0; i<nPeople; i++)
    {
        //NSLog(@"Inside Loop %i", i);

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleArray, i);

        //CRASH HAPPENS HERE
        CFStringRef firstNameStr = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 

...

Tried using non-ASCII first name in addressbook and the app still works fine.

enter image description here

Upvotes: 0

Views: 436

Answers (1)

Mufasa
Mufasa

Reputation: 86

I had the same error, the problem is that:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);

and

CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

are giving a different number of contacts (so in your case nPeople is probably bigger than allPeople, which causes the crash). source doesn't seem to be giving all the contacts in the address book. Changing it to nil solved it for me. Also, to be sure I would do:

nPeople = CFArrayGetCount(allPeople);

The solution is explained by Jokinryou Tsui in this post: ABAddressBookCopyArrayOfAllPeople and ABAddressBookGetPersonCount return different sizes

(This is my first post, so I'm not sure if I broke any rules or followed the right procedure. I hope the answer helps!)

Upvotes: 3

Related Questions