1

I'm trying to get name and last name from Contacts and store the values in a couple of NSString:

CFStringRef cfName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
self.stName = (NSString *)cfName;
CFRelease(cfName);
CFStringRef cfLastname = ABRecordCopyValue(person, kABPersonLastNameProperty);
self.stLastname = (NSString *)cfLastname;
CFRelease(cfLastname);

The problem is, if firstname or lastname are empty in contacts, when I release the CFStringRef the app crash with EXC_BAD_ACCESS (zombie?). If I don't release the CFStringRef I have a leak.

Any suggestion is appreciated.

Max

masgar
  • 1,875
  • 2
  • 20
  • 32
  • Why have you deleted your own answer? It's actually the correct solution. Explanation of `CFRelease` behaviour can be found here: http://stackoverflow.com/a/1608425/653513 – Rok Jarc Jan 21 '13 at 22:14

1 Answers1

0

Yes, CFRelease will crash (halt) if you pass it a NULL argument.

This was allready mentioned by sbooth here.

CFRelease looks like this:

void CFRelease(CFTypeRef cf) {
    if (NULL == cf) HALT;  //this line will get you 
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
    if (CF_IS_COLLECTABLE(cf)) {
        if (CFTYPE_IS_OBJC(cf)) {
            // release the GC-visible reference.
            auto_zone_release(auto_zone(), (void*)cf);
        } else {
            // special-case CF objects for better performance.
            _CFRelease(cf);
        }
        return;
    }
#endif

source

Therefor it's your responcibility to check if you're passing it a NULL or not.

You were correct in your (deleted) answer. Code should look like:

CFStringRef cfName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
self.stName = (NSString *)cfName;
if (self.stName) CFRelease(cfName);
CFStringRef cfLastname = ABRecordCopyValue(person, kABPersonLastNameProperty);
self.stLastname = (NSString *)cfLastname;
if (self.stLastname) CFRelease(cfLastname);

You still might get compiler warnings but this code should work fine.

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124