【发布时间】:2011-10-05 14:20:42
【问题描述】:
通过 X-Code 中的 Leaks 工具运行我的程序,它指出这个函数是我的内存泄漏的主要原因。
+ (NSMutableArray *) getColumns:(NSString *) deviceHtml {
NSMutableArray *ret = [[[NSMutableArray alloc] init] autorelease];
NSRegularExpression *m = [[NSRegularExpression alloc] initWithPattern:@"<td[\\w\\W\\d\\s</>]*?>[\\w\\W\\d\\s]+?</td>" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *results = [m matchesInString:deviceHtml options:NSMatchingCompleted range:NSMakeRange(0, [deviceHtml length])];
[m release];
for (NSTextCheckingResult * res in results) {
NSString *cleaned = [deviceHtml substringWithRange:[res range]];
int firstClose = [cleaned rangeOfString:@">"].location;
int cleanedLength = [cleaned length];
NSString *cleaned1 = [cleaned substringWithRange:NSMakeRange(firstClose+1, cleanedLength-(firstClose+1))];
int closingComment = [cleaned1 rangeOfString:@"</td"].location;
NSString *cleaned2 = [cleaned1 substringWithRange:NSMakeRange(0, closingComment)];
NSString *cleaned3 = [cleaned2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[ret addObject:cleaned3];
}
return ret;
}
特别是这一行,
NSString *cleaned2 = [cleaned1 substringWithRange:NSMakeRange(0, closingComment)];
我不太确定使用 NSCFStrings 和便捷方法进行内存管理,所以我有点卡住了,谁能给我一些指点?
谢谢
【问题讨论】:
-
我没有看到代码有任何明显错误。您确定该函数会导致内存泄漏(而不是分配峰值)吗?您是否尝试在
for循环中使用本地NSAutoreleasePool?
标签: objective-c ios memory-management memory-leaks nsstring