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

Chi*_*ron 0 iphone memory-management objective-c

你能看看我的代码吗:

(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);
}
Run Code Online (Sandbox Code Playgroud)

我在for block之后尝试了CFRelease(person),但是应用程序崩溃了"EXC_BAD_ACCESS".如果我删除CFRelease(人),该应用程序工作.

Jos*_*zzi 9

你没有创建这个人,你只引用了它.在这里插入儿童支持笑话.