(This is more a workaround, perhaps someone else has a better answer.)
The problem does not occur if you define addressBook
not at as property but as an instance variable (perhaps in a class extension):
@interface YourClass () {
ABAddressBookRef addressBook;
}
The problem with a property is that
self.addressBook = ABAddressBookCreate();
// ...
CFRelease(self.addressBook);
is translated to
[self setAddressBook:ABAddressBookCreate()];
// ...
CFRelease([self addressBook]);
so the static analyzer does not "see" at this point that the address book reference is preserved
in some instance variable.
Remark: In dealloc
, you should check that addressBook
is not NULL
if (addressBook != NULL)
CFRelease(addressBook);
to avoid a crash in the case that the variable has not been initialized in viewDidLoad
.
Update: (Motivated by @11684's comment!) You could also keep your property, and use
the associated instance variable for creation and release only:
_addressBook = ABAddressBookCreate();
// ...
if (_addressBook != nil)
CFRelease(_addressBook);
In that case it would make sense to define the property as "read-only".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…