我正在使用 ARC 和 ios sdk 6.0。
我很确定我有一些内存泄漏,我很难追踪。
运行静态分析器后,我收到有关以下两种方法的警告:
+ (id<MXURLRequest>) requestWithURL:(NSURL*)url {
MXASIURLRequest *request = [[MXASIURLRequest alloc] init];
[request setUrl:url];
return request; // STATIC ANALYSER: Potential leak of an object stored into 'request'
}
- (id)parseBody:(NSError *)error {
NSString *contentType = [[_request responseHeaders] objectForKey:@"Content-Type"];
id body = nil;
if ([contentType hasPrefix:@"application/json"] ||
[contentType hasPrefix:@"text/json"] ||
[contentType hasPrefix:@"application/javascript"] ||
[contentType hasPrefix:@"text/javascript"]) {
body = [NSJSONSerialization JSONObjectWithData:[_request responseData] options:NSJSONReadingMutableLeaves error:&error];
} else if ([contentType hasPrefix:@"image/"] ||
[contentType hasPrefix:@"audio/"] ||
[contentType hasPrefix:@"application/octet-stream"]) {
body = [_request responseData];
} else {
body = [[NSString alloc] initWithData:[_request responseData] encoding:NSUTF8StringEncoding];
}
return body; // STATIC ANALYSER : Potential leak of an object stored into 'body'
}
警告如下...
Object leaked: object allocated and stored into 'request' is returned from a method
whose name ('requestWithURL:') does not start with 'copy', 'mutableCopy', 'alloc'
or 'new'. This violates the naming convention rules given in the Memory Management
Guide for Cocoa
Object leaked: object allocated and stored into 'body' is returned from a method
whose name ('parseBody:') does not start with 'copy', 'mutableCopy', 'alloc' or
'new'. This violates the naming convention rules given in the Memory Management
Guide for Cocoa
谁能看到我在这里做错了什么?这些警告是合法的,还是可以忽略?对我来说,这些方法看起来对 ARC 能够处理自动引用计数是有效的。
任何帮助将非常感激。