Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString potential leak

When I build and analyze my project on XCode, I obtain a 'warning' on the following line:

NSString *contactEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailInfo, 0);

The message is: Potential leak on object allocated on line ... and stored into contactEmail.

Is there any error on that line?

UPDATE

I get the same 'warning' with this line of code:

ABMultiValueRef emailInfo = ABRecordCopyValue(person, kABPersonEmailProperty);

But here, I can't do this:

[emailInfo release];

I'm developing for iPhone.

like image 609
VansFannel Avatar asked Apr 18 '26 10:04

VansFannel


2 Answers

ABMultiValueCopyValueAtIndex is a "Copy" function, which follows the "Create Rule". You need to call CFRelease to release it after finish using it.

NSString *contactEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailInfo, 0);
...
if (contactEmail != nil)
  CFRelease((CFTypeRef) contactEmail);
like image 145
kennytm Avatar answered Apr 20 '26 22:04

kennytm


  1. The cast is somewhat pointless.
  2. The line might leak, unless you release or autorelease it somewhere.

Edit: For brevity:

NSString *contactEmail = [(NSString *)ABMultiValueCopyValueAtIndex(emailInfo, 0) autorelease];

(The cast might still be pointless, I'm unsure as to how the compiler would handle trying sending a message directly to a CFTypeRef.)

like image 33
Williham Totland Avatar answered Apr 20 '26 22:04

Williham Totland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!