我有这段代码:
void MyAddressBookExternalChangeCallback(ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
[(id)context reloadSpecifiers];
}
@interface JBPPreferencesListController : PSListController {
ABAddressBookRef addressBook;
}
@end
@implementation JBPPreferencesListController
-(id)init {
if (self = [super init]) {
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// ABAddressBookRegisterExternalChangeCallback(addressBook, MyAddressBookExternalChangeCallback, self);
}
return self;
}
- (id)specifiers {
if (_specifiers == nil) {
NSMutableArray *specs = [NSMutableArray array];
[specs addObjectsFromArray:[self specifiersForContactGroups]];
_specifiers = [specs copy];
}
return _specifiers;
}
-(NSArray *)specifiersForContactGroups {
if (addressBook == NULL) {
return nil;
}
CFIndex groupCount = ABAddressBookGetGroupCount(addressBook);
if (groupCount == 0) {
return nil;
}
CFArrayRef allGroups = ABAddressBookCopyArrayOfAllGroups(addressBook);
if (allGroups == NULL) {
return nil;
}
PSSpecifier* allContactsSpec = [PSSpecifier preferenceSpecifierListItemNamed:[NSString stringWithFormat:@"Favourites %i", groupCount] value:@(-2)];
NSMutableArray *groupSpecs = [NSMutableArray arrayWithObject:allContactsSpec];
for (int i = 0; i < groupCount; i++) {
ABRecordRef currentGroup = CFArrayGetValueAtIndex(allGroups, i);
if (currentGroup != NULL) {
NSString *groupName = (NSString *)ABRecordCopyValue(currentGroup, kABGroupNameProperty);
NSNumber *groupID = @(ABRecordGetRecordID(currentGroup));
PSSpecifier* specifier = [PSSpecifier preferenceSpecifierListItemNamed:groupName value:groupID];
[groupName release];
[groupSpecs addObject:specifier];
}
}
CFRelease(allGroups);
return [[groupSpecs copy] autorelease];
}
-(void)dealloc {
if (addressBook != NULL) CFRelease(addressBook);
[super dealloc];
}
@end
如果我在ABAddressBookRegisterExternalChangeCallback
中取消注释init
行,那么它会在EXC_BAD_ACCESS和分段错误11之前崩溃(在控制器的视图出现在屏幕上之前)。
我没有使用ARC,而且我的手机上有群组(当线路被注释时会出现这些群组)