2

这是我的代码,运行最后一行CFRelease(addressBook),程序崩溃!帮帮我~!

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

NSMutableArray *groups = [NSMutableArray arrayWithCapacity:ABAddressBookGetGroupCount(addressBook)];
NSMutableArray *people = [NSMutableArray arrayWithCapacity:ABAddressBookGetGroupCount(addressBook)];

[(NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllGroups(addressBook)) enumerateObjectsUsingBlock:^(id obj_g, NSUInteger idx, BOOL *stop) {
    ABRecordRef g = (__bridge ABRecordRef)obj_g;
    NSString *gname = CFBridgingRelease(ABRecordCopyCompositeName(g));

    NSMutableArray *gp = [[[NSArray alloc] init] mutableCopy];

    [(NSArray *)CFBridgingRelease(ABGroupCopyArrayOfAllMembers(g)) enumerateObjectsUsingBlock:^(id obj_p, NSUInteger idx, BOOL *stop) {
        ABRecordRef p = (__bridge ABRecordRef)obj_p;
        ABMultiValueRef numbers = ABRecordCopyValue(p, kABPersonPhoneProperty);

        NSString *pname = CFBridgingRelease(ABRecordCopyCompositeName(p));

        [(NSArray *) CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(numbers)) enumerateObjectsUsingBlock:^(id obj_n, NSUInteger idx, BOOL *stop) {
            NSString *pnumber = CFBridgingRelease(CFBridgingRetain(obj_n));

            MasMsContact *new_c = [[MasMsContact alloc] init];
            new_c.name = pname;
            new_c.number = pnumber;
            new_c.group = gname;

            [gp addObject:new_c];
        }];

        CFRelease(numbers);
        CFRelease(p);
    }];

    CFRelease(g);

    [people addObject:[gp copy]];
    [groups addObject:gname];
}];

#warning crash when release addressBook, why ???
if (addressBook) CFRelease(addressBook);

崩溃时的Xcode

4

2 回答 2

5

当您释放它们的桥接引用然后从该枚举块中弹出时,您并不拥有它们obj_pobj_g因此它们被过度释放。如果您使用Create或访问它们,您只有拥有核心基础对象Copy

于 2012-12-26T03:19:53.460 回答
0

我在谷歌上搜索这个iPhone SDK:EXC_BAD_ACCESS with CFRelease for ABAddressBookRef

我有同样的问题,过度释放gp.

于 2012-12-26T03:08:24.237 回答