调用CFRelease时出现空指针参数

The*_*ner 1 iphone objective-c xcode4

我似乎无法构造这种方法,所以当我分析项目时它不会抱怨.

它抱怨我如何释放人物对象.

- (NSArray *)getAllContacts {

    NSMutableArray *result = [NSMutableArray array];
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);

    CFRelease(addressBook);

    NSArray *peopleArray = (NSArray *)people;

    //  Return if there are no contacts in the address book
    if (peopleArray && peopleArray.count > 0) {

        for (int i = 0; i <= peopleArray.count -1; i++) {

            ABRecordRef person = [peopleArray objectAtIndex:i];
            ABRecordID sourceID = ABRecordGetRecordID(person);

            TableViewControllerItem *item = [AddressBookModel createTableViewControllerItemFromABRecordID:[NSString stringWithFormat:@"%i", sourceID]];
            [result addObject:item];
        }
        CFRelease(people); //If I put the release here I get a potential leak of people
    }

    CFRelease(people); //If I put the release here I get a null pointer argument in call to CFRelease

    return [NSArray arrayWithArray:result];
}
Run Code Online (Sandbox Code Playgroud)

Emp*_*ack 8

// Remove the CFRelease() inside the if-block 
Run Code Online (Sandbox Code Playgroud)

并在return语句之前修改CFRelease(),如下所示,

if (peopleArray) CFRelease(people);
return [NSArray arrayWithArray:result];
Run Code Online (Sandbox Code Playgroud)