2011-10-18 58 views
1

我似乎無法構造此方法,因此當我分析項目時,它不會發出抱怨。調用CFRelease中的空指針參數

這是抱怨我如何釋放人的對象。

- (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]; 
} 

回答

8
// Remove the CFRelease() inside the if-block 

並修改CFRelease()語句之前是這樣,

if (peopleArray) CFRelease(people); 
return [NSArray arrayWithArray:result];