使用ABRecord和ARC可能发生泄漏

时间:2012-09-28 15:25:03

标签: ios memory-leaks ios6 clang-static-analyzer

我有这段代码:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
      shouldContinueAfterSelectingPerson:(ABRecordRef)personRecord
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier
{
    //TODO: release phoneNumberProperty when done
    ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(personRecord, kABPersonPhoneProperty);
    int selectedPhNum = ABMultiValueGetIndexForIdentifier(phoneNumberProperty, identifier);//Turns identifier into index
    NSString *txtNum = (NSString *)CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumberProperty, selectedPhNum));
    //NSLog(@"Selected ph num = %@", txtNum);
    [[self toTextField] setText:txtNum];
    phoneNumberProperty = nil;
    [self dismissViewControllerAnimated:YES completion:NULL];

静态分析仪说有潜在的泄漏。我知道我需要释放phoneNumberProperty但是如何?我正在使用ARC,因此[phoneNumberProperty release]无法正常工作。将它设置为零并仍然抱怨。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

使用CFRelease函数释放ABMultiValueRef变量:

...
if (phoneNumberProperty)
    CFRelease(phoneNumberProperty);