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 的内存管理和便捷方法,所以我有点卡住了,有人能给我一些指示吗?
谢谢
Running my program through the Leaks tool in X-Code, it points to this function as the main cause of my memory 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;
}
Specifically this line,
NSString *cleaned2 = [cleaned1 substringWithRange:NSMakeRange(0, closingComment)];
I'm not really sure about memory management with NSCFStrings and convenience methods so I'm a little stuck, can anyone give me a few pointers?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,该方法不应该是
getColumns:
,而应该是columnsForDevice:
之类的方法。get*
作为前缀在 Cocoa 中具有非常特定的含义,但事实并非如此。其次,Leaks 工具会向您显示泄漏分配的位置,而不是泄漏实际发生的位置。
如果返回的数组在其他地方过度保留,这将是泄漏的根源。
First, the method should not be
getColumns:
but, say, something likecolumnsForDevice:
.get*
as a prefix has a very specific meaning in Cocoa and this isn't it.Secondly, the Leaks instrument shows you where a leak was allocated, not where the leak may actually be happening.
If the returned array is over-retained elsewhere, that would be the source of your leak.