调用CFRelease时崩溃(ABAddressBookRef ref)

时间:2012-12-26 02:45:27

标签: ios

这是我的代码,运行最后一行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 when crash

2 个答案:

答案 0 :(得分:5)

您不拥有obj_pobj_g,因此当您释放其桥接引用然后弹出该枚举块时,它们会被过度释放。如果您使用CreateCopy

访问核心基础对象,则只拥有核心基础对象

答案 1 :(得分:0)

我这是谷歌iPhone SDK: EXC_BAD_ACCESS with CFRelease for ABAddressBookRef

我遇到同样的问题,而不是发布gp