Free ABMultiValueRef Object

In my application, a static analyzer indicates a leak in the following code:

ABMultiValueRef phone = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty); NSString *mobilephone= (NSString*)ABMultiValueCopyValueAtIndex (phone,0); 

similarly whenever i use this function ABRecordCopyValue it indicates a leak

I tried to release it using the [phone release]; method [phone release]; but I get a compiler warning “invalid receiver type“ abmultivalueref ”. What is the correct way to free this?

+4
source share
3 answers

It looks like you are confusing NS data types with CF data types. Address book methods typically return basic foundation ( CF ) objects. These facilities are free bridges, which means they can be used interchangeably with NS types.

When using basic base objects, any method with a “copy” in its name returns an object that you later need to free using CFRelease . Only if you apply it to the NS equivalent can you use - release .

Thus, your code can be written as one 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); 
+5
source

Have you tried using CFRelease(phone); ?
Since ABMultiValueCopyValueAtIndex not NSString, it is a CFStringRef

+1
source

Using __bridge_transfer ensures that ARC will free the object for you. Using __bridge means you must free the returned object manually.

+1
source

Source: https://habr.com/ru/post/1343735/


All Articles