为什么我试图发布时会收到“EXC_BAD_ACCESS”?

时间:2010-11-07 22:51:41

标签: iphone objective-c memory-management

请你看看我的代码:

(void)loadContactsFromAddressBook {
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex count = ABAddressBookGetPersonCount(addressBook);
ABRecordRef person;

for (int i = 0; i < count; i++) {
    person = CFArrayGetValueAtIndex(allPeople, i);
    CFStringRef cfStr = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString* firstName = (NSString*) cfStr;
    [contacts addObject:firstName]; 
    CFRelease(cfStr);
}

CFRelease(addressBook);
CFRelease(allPeople);
}

我在for block之后尝试了CFRelease(person),但是应用程序崩溃了“EXC_BAD_ACCESS”。如果我删除CFRelease(person),该应用程序将起作用。

1 个答案:

答案 0 :(得分:9)

你没有创建这个人,你只引用了它。 在此处插入子支持笑话。