release ABMultiValueRef object
In my app, static analyser points a leak in the following code:
ABMultiValueRef phone = (NSString *)ABRecordCopyValue(perso开发者_运维技巧n, kABPersonPhoneProperty);
NSString *mobilephone= (NSString*)ABMultiValueCopyValueAtIndex (phone,0);
similarly whenever i use this function ABRecordCopyValue it points a leak
I tried to release it by [phone release];
method, however I am getting a compiler warning "invalid receiver type 'abmultivalueref'". What is the proper way to release this ?
It looks like you are confusing the NS
data types with the CF
data types. The address book methods typically return core foundation (CF
) objects. These objects are toll-free bridged, which means they can be used interchangeably with NS
types.
When using core foundation objects, any method with 'copy' in its name will return an object that you later need to release using CFRelease
. Only if you cast it to its NS
equivalent can you use - release
.
So your code could be written as either of the following:
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *mobilephone = (NSString *)ABMultiValueCopyValueAtIndex(phone, 0);
// other code
[mobilephone release];
or
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFStringRef mobilephone = ABMultiValueCopyValueAtIndex(phone, 0);
// other code
CFRelease(mobilephone);
Have you tried with CFRelease(phone);
?
Because ABMultiValueCopyValueAtIndex
is not a NSString, it's a CFStringRef
Using __bridge_transfer
ensures that ARC will release the object for you.
Using __bridge
means you must release the returned object manually.
精彩评论