内存泄漏与SubstringWithRange NSString

通过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和便捷方法的内存pipe理,所以我有点卡住了,谁能给我几个指针?

谢谢

首先,该方法不应该是getColumns:但是,比如columnsForDevice: get*作为前缀在Cocoa中有一个非常具体的含义,这不是它。

其次,泄漏仪器显示泄漏的分配位置 ,而不是实际发生泄漏的位置

如果返回的数组在其他地方被过度保留,那将是你泄漏的源头。