XCode内存泄漏问题

时间:2013-01-15 03:47:59

标签: ios objective-c memory-leaks abrecord

我即将结束我的项目,但在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:@""];

任何人都可以帮我解决这个问题吗?我不相信它会瘫痪,但我不想让Apple有理由拒绝我的应用程序。谢谢。

...最佳SL

1 个答案:

答案 0 :(得分:4)

__bridge_transfer的{​​{1}}返回值外,您在phoneNumbers处使用{。}}。

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

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

添加phoneNumbers将手动释放内存。

例如:

CFRelease(phoneNumbers)