1

我有这个代码:

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

如果我取消注释ABAddressBookRegisterExternalChangeCallbackinit,那么它会崩溃(在控制器的视图出现在屏幕上之前)并出现 EXC_BAD_ACCESS 和分段错误,11。

我没有使用 ARC,并且我的手机上有组(当线路被注释掉时它们会出现)

4

0 回答 0