I use the following code to remove all contacts I have inserted in the address book, depending on the group
they have been added to :
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray* contactsArray = (__bridge NSArray *)(ABGroupCopyArrayOfAllMembers([self ABGetAddressGroup:addressBook]));
if (contactsArray.count) {
for (NSUInteger index = 0 ; index < contactsArray.count; index++) {
ABRecordRef person = (__bridge ABRecordRef)(contactsArray[index]);
ABAddressBookRemoveRecord(addressBook, person, NULL);
}
CFRelease((__bridge CFTypeRef)contactsArray);
NSArray *groups = (__bridge NSArray *) ABAddressBookCopyArrayOfAllGroups(addressBook);
for (id _group in groups) {
NSString *currentGroupName = (__bridge NSString*) ABRecordCopyValue((__bridge ABRecordRef)(_group), kABGroupNameProperty);
if ([[self getDatabase] isEqualToString:currentGroupName]) {
ABAddressBookRemoveRecord(addressBook, (__bridge ABRecordRef)(_group), NULL);
}
CFRelease((__bridge CFTypeRef)(currentGroupName));
}
CFRelease((__bridge CFTypeRef)(groups));
}
if (ABAddressBookHasUnsavedChanges(addressBook)) {//potential leak of an object stored in contactsArray
//Save recent changes
ABAddressBookSave(addressBook, NULL);
} else {
NSLog(@"Nothing to do here, let's eat cake");
}
CFRelease(addressBook);
The Analyzer show a warning (if that's what it's called) in the ABAddressBookHasUnsavedChanges
...potential leak of an object stored in contactsArray
. What do I need to do to get rid of this?
If use CFRelease
as follows:
CFRelease((__bridge CFTypeRef)(contactsArray));
the analyzer says : Reference-counted object is used after it is released
, even if it released at the end of the non-returning method, and the app crashes.
What am I doing wrong here? How do I get rid of such memory leaks?