2

我的项目快结束了,尽管在 XCode 中分析我的项目后,它向我表明这一行存在内存泄漏:

http://i.imgur.com/uTkbA.png

以下是相关代码的文本版本:

- (void)displayPerson:(ABRecordRef)person
{
    NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);


    NSMutableString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

    //NSLog(@"%@", fullName);

    NSString* phoneNum = nil;
    ABMultiValueRef phoneNumbers;
    phoneNumbers = ABRecordCopyValue(person,
                                                     kABPersonPhoneProperty);
    if (ABMultiValueGetCount(phoneNumbers) > 0) {
        phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
    } else {
        phoneNum = @"Unknown";
    }

    NSLog(@"First name is %@ and last name is %@", firstName, lastName);
    NSLog(@"Phone is %@", phoneNum);

    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"(" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@")" withString:@""];

谁能帮我解决这个问题?我不相信这会造成严重后果,但我不想给苹果一个理由拒绝我的应用程序从商店。谢谢你。

最好的...SL

4

1 回答 1

4

__bridge_transfer除了phoneNumbers来自ABRecordCopyValue. _

您需要将所有权转让phoneNumbers给 ARC 或手动释放内存。

更新:仔细研究了这个问题后,我不确定您是否可以将所有权转让给 ARC,请参阅__bridge_transfer 和 ABRecordCopyValue: 以及 ARC了解更多详细信息。

添加CFRelease(phoneNumbers)会手动释放内存。

例如:

NSString* phoneNum = nil;
ABMultiValueRef phoneNumbers;
phoneNumbers = ABRecordCopyValue(person,
                                                 kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
    phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
    phoneNum = @"Unknown";
}

CFRelease(phoneNumbers);
于 2013-01-15T03:55:13.820 回答