Crash in ABAddPropertiesAndTypes

Q: I've tried adding custom property types to Address Book, but my application crashes when I call ABAddPropertiesAndTypes. What's wrong?

A: When constructing a dictionary of new properties to pass in to ABAddPropertiesAndTypes, you define the datatype of the new property (for example, kABStringProperty. These identifiers are of type ABPropertyType, but ABAddPropertiesAndTypes expects them to be wrapped in CFNumber objects. Code that explicitly adds the type constant into the dictionary of new properties will fail.

Two snippets of code are listed below to demonstrate the specifics of this problem. The code in Listing 1 will fail when run; the code in Listing 2 demonstrates how to correctly call ABAddPropertiesAndTypes.

Listing 1: The wrong way to call ABAddPropertiesAndTypes

ABAddressBookRef  abRef = ABGetSharedAddressBook();
ABPropertyType    type  = kABStringProperty;

CFMutableDictionaryRef propsAndTypes = CFDictionaryCreateMutable(NULL, 1, NULL, NULL);

// This is the problem.  kABStringProperty is added as-is, when it should be wrapped in a CFNumber.
CFDictionaryAddValue(propsAndTypes, CFSTR("my.new.property"), &type);
printf("Added %d properties\n", ABAddPropertiesAndTypes(abRef, kABPersonRecordType, propsAndTypes));

CFRelease(propsAndTypes);

Listing 2: The right way to call ABAddPropertiesAndTypes

ABAddressBookRef  abRef = ABGetSharedAddressBook();
ABPropertyType    type  = kABStringProperty;

CFMutableDictionaryRef propsAndTypes = CFDictionaryCreateMutable(NULL, 1, NULL, NULL);

// Note the additional line here, and the passing of a CFNumberRef instead of an ABPropertyType
CFNumberRef  numberRef = CFNumberCreate(NULL, kCFNumberSInt32Type, &type);
CFDictionaryAddValue(propsAndTypes, CFSTR("my.new.property"), numberRef);
printf("Added %d properties\n", ABAddPropertiesAndTypes(abRef, kABPersonRecordType, propsAndTypes));

CFRelease(numberRef);
CFRelease(propsAndTypes);

Document Revision History

DateNotes
2005-04-04Fixed a typographical error.
2005-03-15How to correctly add custom Address Book properties using ABAddPropertiesAndTypes

Posted: 2005-04-04


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.