1

I am using the following code to initialize the address book in my application :-

@property(nonatomic,assign) ABAddressBookRef addressBookRef;
self.addressBookRef=ABAddressBookCreateWithOptions(NULL, NULL);

However, the xcode analyzer is giving me the following message :-

call to function 'ABAddressBookCreateWithOptions' returns a Core Foundation Object with a +1 retain count

Now, as far as I have read, I can "remove" the above warning by the following 2 methods :-

  • Release addressBookRef by using CFRelease
  • Instead of self.addressBookRef, use _addressBookRef in second statement.

However, which one will be a better way and why ?

Max
  • 4,067
  • 1
  • 18
  • 29

1 Answers1

2

All CoreFoundation API with create in their name return a CF object with a +1 retain count. You then need to release the new CF object when finished with it. Example with your naming:

CFRelease(self.addressBookRef);

I strongly recommend reading this answer too:iOS 6 Address Book not working?

Community
  • 1
  • 1
Robotic Cat
  • 5,899
  • 4
  • 41
  • 58
  • right. But if we directly use _addressBookRef, why doesn't the xcode analyzer give a warning regarding its release ? – Max Sep 13 '13 at 00:37
  • @Max: No idea why it doesn't give a warning - sorry. Normally, if I'm using a CF object, I create it, use it and then release it rather than retain a reference to it. – Robotic Cat Sep 13 '13 at 01:01
  • what if in my method I need to return this addressBookRef? should I do myAddressBookRef = self.addressBookRef before CFRelease(self.addressBookRef) or what? – xialin Mar 24 '14 at 03:32
  • @xialinZZZ:I don't know what you mean - once you have the reference in a property you don't need to return it. If you have a problem you should ask a question. – Robotic Cat Mar 24 '14 at 12:30
  • after this comment I did post another question here: http://stackoverflow.com/questions/22600766/whats-the-correct-way-of-releasing-an-object-when-it-also-needs-to-be-returned – xialin Mar 24 '14 at 12:40