I have, under ARC, a tableview controller that reads data from the address book for every shown tableview cell. Since for performance reasons I cannot open the address book for every call to tableView:cellForRowAtIndexPath:
, I open it once in viewDidLoad
and to store the reference to it in a @property (nonatomic) ABAddressBookRef addressBookRef;
It will be released with CFRelease(self.addressBookRef);
in the dealloc
method of the tableview controller.
This seems to me to be correct, but the static analyzer complains about a "Potential leak of an object" in viewDidLoad
in the line of the if
statement below:
- (void)viewDidLoad{
[super viewDidLoad];
CFErrorRef error = nil;
self.addressBookRef = ABAddressBookCreateWithOptions (NULL, &error);
if (self.addressBookRef == nil) {
NSLog(@"%@: %@: Could not open addressbook", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
...
Do I something wrong, or how could I get rid of the warning?
You could override setAddressBookRef:
so that it retains the address book ref, and release it after assign the property.
Something like this. You'll want to check for NULL, since calling CFRetain/Release with it will cause a runtime error.
- (void)setAddressBookRef:(ABAddressBookRef)addressBook
{
CFRetain(addressBook);
CFRelease(_addressBookRef);
_addressBookRef = addressBook;
}