Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

release CFString

I use this code to get the last name of ABPerson

CFStringRef lastNameRef = ABRecordCopyValue((ABRecordRef)personRecordRef, kABPersonLastNameProperty);
NSString *friendLastName = (NSString*)lastNameRef;
CFRelease(lastNameRef);

it work fine when the value of last name is not equal to NULL but when the this value is NULL the application crash at the third line because I try to relese NULL

the question is witch is the best way to releasing the CFString in this case without causing the crash of the application

like image 976
iArezki Avatar asked Aug 01 '26 02:08

iArezki


2 Answers

Just use an if to check for NULL.

if (lastNameRef != NULL)
    CFRelease(lastNameRef);
like image 60
Hampus Nilsson Avatar answered Aug 02 '26 18:08

Hampus Nilsson


CFRelease is old C style code. One should check for NULL before calling CFRelease as also set lastNameRef to NULL after calling CFRelease.

if (lastNameRef != NULL) { CFRelease(lastNameRef); lastNameRef = NULL; }

like image 39
Aditya Kumar Pandey Avatar answered Aug 02 '26 19:08

Aditya Kumar Pandey