NSLinguisticTagger 内存泄漏

发布于 2024-12-24 16:01:53 字数 2353 浏览 1 评论 0原文

我一直在 Xcode 4.2 中摆弄 iOS 5.0 的新 NSLinguisticTagger。我使用此函数的目标是获取地址簿记录,然后以 NSString 的形式输出复合名称,有点像 ABRecordCopyCompositeName 所做的那样,但考虑到东亚语言和匈牙利语的命名顺序(最后一个而不是第一个最后) )。这是函数:

NSString *text = [self getLocalizedFullNameOfRecord:[contacts objectAtIndex:indexPath.section];


- (NSString *) getLocalizedFullNameOfRecord:(ABRecordRef) person
{
    NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *middleName = ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSString *prefix = ABRecordCopyValue(person, kABPersonPrefixProperty);
    NSString *suffix = ABRecordCopyValue(person, kABPersonSuffixProperty);
    NSString *fullName = @"";

    __block BOOL Asian;
    // Apologies to all Hungarians who aren't actually Asian
    __block NSArray *asianLanguages = [NSArray arrayWithObjects:@"zh-Hant", @"zh-Hans", @"ja", @"ko", @"hu", @"vi", nil];

    [firstName enumerateLinguisticTagsInRange:NSMakeRange(0, firstName.length) scheme: NSLinguisticTagSchemeLanguage options: NSLinguisticTaggerOmitWhitespace orthography: nil usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop){
        if ([asianLanguages containsObject:tag])
            Asian = YES;
        else
            Asian = NO;
    }];

    if(prefix)
        fullName = [fullName stringByAppendingFormat:@"%@ ", prefix];
    if(Asian && lastName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", lastName];
    else if(firstName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", firstName];
    if(middleName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", middleName];
    if(Asian && firstName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", firstName];
    else if(lastName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", lastName];
    if(suffix)
        fullName = [fullName stringByAppendingFormat:@"%@", suffix];

    [firstName release];
    [middleName release];
    [lastName release];
    [prefix release];
    [suffix release];

    return fullName;
}

Instruments 告诉我,在该函数的每次迭代中,我在 enumerateLinguisticTagger 上泄漏了大约 16 到 32 个字节(显然不是块部分)。由于 NSLinguisticTagger 的在线资源仅限于其类参考和单个教程,因此我不知道从哪里以及如何开始寻找泄漏。

请帮忙?

I've been fiddling in Xcode 4.2 with iOS 5.0's new NSLinguisticTagger. My objective with this function is to take in an address book record and then spit out a composite name as an NSString, sort of like what ABRecordCopyCompositeName does, but taking into account naming order for East Asian languages and Hungarian (last first instead of first last). Here's the function:

NSString *text = [self getLocalizedFullNameOfRecord:[contacts objectAtIndex:indexPath.section];


- (NSString *) getLocalizedFullNameOfRecord:(ABRecordRef) person
{
    NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *middleName = ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSString *prefix = ABRecordCopyValue(person, kABPersonPrefixProperty);
    NSString *suffix = ABRecordCopyValue(person, kABPersonSuffixProperty);
    NSString *fullName = @"";

    __block BOOL Asian;
    // Apologies to all Hungarians who aren't actually Asian
    __block NSArray *asianLanguages = [NSArray arrayWithObjects:@"zh-Hant", @"zh-Hans", @"ja", @"ko", @"hu", @"vi", nil];

    [firstName enumerateLinguisticTagsInRange:NSMakeRange(0, firstName.length) scheme: NSLinguisticTagSchemeLanguage options: NSLinguisticTaggerOmitWhitespace orthography: nil usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop){
        if ([asianLanguages containsObject:tag])
            Asian = YES;
        else
            Asian = NO;
    }];

    if(prefix)
        fullName = [fullName stringByAppendingFormat:@"%@ ", prefix];
    if(Asian && lastName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", lastName];
    else if(firstName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", firstName];
    if(middleName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", middleName];
    if(Asian && firstName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", firstName];
    else if(lastName)
        fullName = [fullName stringByAppendingFormat:@"%@ ", lastName];
    if(suffix)
        fullName = [fullName stringByAppendingFormat:@"%@", suffix];

    [firstName release];
    [middleName release];
    [lastName release];
    [prefix release];
    [suffix release];

    return fullName;
}

Instruments tells me that I am leaking some 16 to 32 bytes with every iteration of this function, on the enumerateLinguisticTagger (and not the blocks part, apparently). Since online resources for NSLinguisticTagger is limited to its class reference and a single tutorial, I have no idea where and how to start looking for the leak.

Help please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2024-12-31 16:01:53

我也有同样的问题。就我而言,当字符串有换行符(\n 或 \r)时,就会发生泄漏。

I had the same problem. In my case, leaks occurred when string has line breaks (\n or \r).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文