I have a class that accesses the address book, if I have the following code then I get warnings from the analyzer, one is for a leak in the init method and another is saying the class doesn’t own the object when its releasing it in the dealloc.
I presume the 2nd warning is due to declaring the address book ref as assign? But its not possible to make it strong as this generates a compilation warning.
What is the correct way of dealing with the address book as a property?
@property (assign, nonatomic) ABAddressBookRef addressBook;
..
- (id) init
{
self = [super init];
if (self)
{
if (ABAddressBookCreateWithOptions)
{
// iOS 6 onwards
self.addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
}
else
{
// < iOS 6
self.addressBook = ABAddressBookCreate();
}
}
return self;
}
- (void) dealloc
{
if (self.addressBook)
{
CFRelease(self.addressBook);
}
}
It’s a false positive, you are correctly balancing the initial +1 of the
retainCountwith a theCFReleasecall in the dealloc method.You can get rid of the warnings by using the ivar instead of the property to initialize and release the pointer.