问题
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?
回答1:
I had the same problem. In my case, leaks occurred when string has line breaks (\n or \r).
来源:https://stackoverflow.com/questions/8707521/nslinguistictagger-memory-leak