EXC_BAD_ACCESS when adding contacts from Addressbook?

I have the following code :

ABAddressBookRef ab; ab = ABAddressBookCreate(); int len = (int) ABAddressBookGetPersonCount(ab); int i; for(i = 1; i < (len + 1); i++) { ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,(ABRecordID) i); CFStringRef firstName, lastName; firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); static char* fallback = ""; int fbLength = strlen(fallback); int firstNameLength = fbLength; bool firstNameFallback = true; int lastNameLength = fbLength; bool lastNameFallback = true; if (firstName != NULL) { firstNameLength = (int) CFStringGetLength(firstName); firstNameFallback = false; } if (lastName != NULL) { lastNameLength = (int) CFStringGetLength(lastName); lastNameFallback = false; } if (firstNameLength == 0) { firstNameLength = fbLength; firstNameFallback = true; } if (lastNameLength == 0) { lastNameLength = fbLength; lastNameFallback = true; } firstNameString = malloc(sizeof(char)*(firstNameLength+1)); lastNameString = malloc(sizeof(char)*(lastNameLength+1)); if (firstNameFallback == true) { strcpy(firstNameString, fallback); } else { CFStringGetCString(firstName, firstNameString, 10*CFStringGetLength(firstName), kCFStringEncodingASCII); } if (lastNameFallback == true) { strcpy(lastNameString, fallback); } else { CFStringGetCString(lastName, lastNameString, 10*CFStringGetLength(lastName), kCFStringEncodingASCII); } printf("%d.\t%s %s\n", i, firstNameString, lastNameString); NSString *fname= [NSString stringWithFormat:@"%s",firstNameString]; NSString *lname= [NSString stringWithFormat:@"%s",lastNameString]; [dict setValue:fname forKey:@"fname"]; [dict setValue:lname forKey:@"lname"]; [self.arrname addObject:[dict copy]]; if (firstName != NULL) { CFRelease(firstName); } if (lastName != NULL) { CFRelease(lastName); } free(firstNameString); free(lastNameString); }

it working well for first time.

But When i delete record from contact list and then try to add record my App crase at the following statement.

firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);

Can anyone solve this problem ?? Any Idea is greatly appreciated.


There's no guarantee that the valid ABRecordID starts at 1 and ends at ABAddressBookGetPersonCount(addressBook). You <strong>can't</strong> use the for-loop using ABRecordID.

Instead, obtain the CFArray containing all the people using ABAddressBookCopyArrayOfAllPeople and iterate on it.

Another comment is that you shouldn't use C string; most of the things can be done using the API of CFString and NSString, which supports Unicode out of the box. By getting the C string specifying kCFStringEncodingASCII, you're basically destroying letters like é or ü, ગુજરાતી or 案. (Note that CFStringGetCString with kCFStringEncodingASCII is quite picky and removes characters not in the ASCII; it doesn't give you UTF8 representation of the string.) There're many people whose name contains non-ASCII characters. So, please do learn CFString and NSString methods. Note that a CFStringRef and an NSString* can be freely interchanged.


  • how to trim audio file with specific time from text file by using SoX in Mac terminal?
  • UNet randomly disconnects
  • Why Android LocationManager has long delay before location updates start if setting accuracy
  • How to install the guess_language package in Python
  • c# - how to improve this “file explorer” startup speed?
  • How to handle concurrency with StorageFile operations?
  • My 'Explicit Wait' doesn't work but 'Implicit Wait' Works?
  • Problem with wininet C++
  • scanf equivalent with arrow key support in c
  • What is better way to delete file with condition
  • how to enable Wi-Fi and search for networks on windows phone emulator
  • Check if a program exists in bash
  • Fiware response 503 - Service unavailable
  • Automapper Enum Description Attribute
  • Shell Function string variables result blank after While loop and case?
  • Pandas - Counting quantity of commas in character field
  • BASH: Ctrl+C during input breaks current terminal
  • why does bash tell me that the command if is not found when I run the following code?
  • Git cherry pick those commits that contain a keyword (tracking id)
  • send() crashes my program
  • Conflicting Types Error
  • Can we reuse allocated memory
  • Salted sha512 in C, cannot synchronise with Symfony2's FOSUserBundle
  • Delete multiple lines - from “patternA” match, through second occurrence of “patternB”
  • Getting segmentation fault while using malloc
  • Apple Reachability Notifications for Network OR Wi-Fi
  • C#, “Object reference not set to an instance of an object.” error
  • How to work with AMMediaType for video filters
  • How to unpack 32bit integer packed in a QByteArray?
  • allocating memory to an array of string
  • C# fibonacci function returning errors
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Why is the size of this struct 32?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?