Objective c 具有子字符串WithRange NSString的内存泄漏
通过X代码中的泄漏工具运行我的程序,它指出这个函数是导致内存泄漏的主要原因Objective c 具有子字符串WithRange NSString的内存泄漏,objective-c,ios,memory-management,memory-leaks,nsstring,Objective C,Ios,Memory Management,Memory Leaks,Nsstring,通过X代码中的泄漏工具运行我的程序,它指出这个函数是导致内存泄漏的主要原因 + (NSMutableArray *) getColumns:(NSString *) deviceHtml { NSMutableArray *ret = [[[NSMutableArray alloc] init] autorelease]; NSRegularExpression *m = [[NSRegularExpression alloc] initWithPatt
+ (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;
}
我对NSCFString和便利方法的内存管理不是很确定,所以我有点卡住了,有人能给我一些建议吗
谢谢首先,方法不应该是
getColumns:
,而是类似于columnsForDevice:
get*
作为前缀在Cocoa中有非常特殊的含义,但事实并非如此
其次,泄漏仪器显示泄漏分配的位置,而不是泄漏实际发生的位置
如果返回的数组在其他地方被过度保留,那将是您的泄漏源。我看不出代码有任何明显的错误。您确定该函数会导致内存泄漏(而不是分配峰值)吗?您是否尝试在
for
循环中使用本地NSAutoreleasePool
?
NSString *cleaned2 = [cleaned1 substringWithRange:NSMakeRange(0, closingComment)];