I have the method which causes a memory leak as shown by the instrument:
-(BOOL)checkIfGroupExistWithName:(NSString*)groupName
{
BOOL hasGroup = NO;
//checks to see if the group is created ad creats group for Handheld contacts
CFErrorRef error = NULL;
ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, &error);
CFIndex groupCount = ABAddressBookGetGroupCount(ab);
CFArrayRef allGroups = ABAddressBookCopyArrayOfAllGroups(ab);
for (int i=0; i<groupCount; i++) {
ABRecordRef group = CFArrayGetValueAtIndex(allGroups, i);
CFStringRef CFcurrentGroupName = ABRecordCopyCompositeName(group);
NSString *currentGroupName = (__bridge_transfer NSString *)CFcurrentGroupName;
if ([currentGroupName isEqualToString:groupName]) {
//!!! important - save groupID for later use
groupId = ABRecordGetRecordID(group);
hasGroup = YES;
i = (int) groupCount;
}
CFRelease(CFcurrentGroupName);
CFRelease(group);
}
return hasGroup;
}
If I use CFRelease(ab); before return hasGroup, it crashes. I couldn't understand what is happening here.