0

I am reading records from the AddressBook using the Apple provided API.

I am still getting my head around memory management and so CFStrings are confusing me at the moment.

This is how I am getting the properties:

//Get Basic properties
NSString* firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSNumber* record = [NSNumber numberWithInt:ABRecordGetRecordID(person)];

//Build Full Name
NSString* fullName=[self fullNameWith:firstName and:lastName];

//Get Phone number and Label
ABMultiValueRef phone = ABRecordCopyValue(person, property);
    //Turn identifier into index
CFIndex index = ABMultiValueGetIndexForIdentifier(phone, identifier);
    //Get the value and the label using the index
NSString *value =(NSString *)ABMultiValueCopyValueAtIndex(phone, index);
CFStringRef label = ABMultiValueCopyLabelAtIndex(phone, index);
    //Get the localized value of hte label
NSString * localizedLabel = (NSString *)ABAddressBookCopyLocalizedLabel(label);

After that I use the values, the only thing is that I dont know if I should release them or not.

I would appreciate an answer that also helped me understand memory management better or that points me to the right direction.

Thank you!

Girish
  • 4,692
  • 4
  • 35
  • 55
Zebs
  • 5,378
  • 2
  • 35
  • 49

1 Answers1

5

The rule of thumb for Core Foundation is that any functions that include Copy or Create in their name will return an object that you are responsible for releasing. Apple's Memory Management Guide for Core Foundation explains this in a bit more detail.

Justin Spahr-Summers
  • 16,893
  • 2
  • 61
  • 79
  • Thank you! Just to clarify, when you do the cast from CFString to NSString do you do [string release] or CFRelease(string)? – Zebs Nov 22 '10 at 04:25
  • Actually, in this case, they'll behave the exact same because CFString and NSString are "toll-free bridged." I would use whichever one is clearest and most easily understood for your code and whoever may read it. – Justin Spahr-Summers Nov 22 '10 at 04:27