NSLinguisticTagger内存泄漏 [英] NSLinguisticTagger Memory Leak

查看:122
本文介绍了NSLinguisticTagger内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用iOS 5.0的新NSLinguisticTagger摆弄Xcode 4.2。我使用此函数的目的是接收一个地址簿记录,然后将一个复合名称作为NSString吐出,类似于ABRecordCopyCompositeName所做的,但考虑到东亚语言和匈牙利语的命名顺序(最后一个而不是最后一个) )。这是函数:

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

乐器告诉我每次迭代时我会泄漏大约16到32个字节这个函数,在enumerateLinguisticTagger(而不是块部分,显然)。由于NSLinguisticTagger的在线资源仅限于其类引用和单个教程,我不知道在哪里以及如何开始寻找泄漏。

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.

请帮忙吗?

推荐答案

我遇到了同样的问题。在我的情况下,当字符串有换行符(\ n或\r)时发生泄漏。

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

这篇关于NSLinguisticTagger内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆