简体   繁体   English

NSLinguisticTagger内存泄漏

[英]NSLinguisticTagger Memory Leak

I've been fiddling in Xcode 4.2 with iOS 5.0's new NSLinguisticTagger. 我一直在使用iOS 5.0的新NSLinguisticTagger摆弄Xcode 4.2。 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). 我使用此函数的目的是接收一个地址簿记录,然后将一个复合名称作为NSString吐出,类似于ABRecordCopyCompositeName所做的,但考虑到东亚语言和匈牙利语的命名顺序(最后一个而不是最后一个) )。 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). Instruments告诉我,在enumerateLinguisticTagger(而不是块部分,显然)上,每次迭代此函数时,我会泄漏大约16到32个字节。 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. 由于NSLinguisticTagger的在线资源仅限于其类引用和单个教程,我不知道在哪里以及如何开始寻找泄漏。

Help please? 请帮助?

I had the same problem. 我有同样的问题。 In my case, leaks occurred when string has line breaks (\\n or \\r). 在我的情况下,当字符串有换行符(\\ n或\\ r)时发生泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM