Xcode静态分析器在使用ARC时抱怨潜在的泄漏

我正在使用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能够处理自动引用计数是有效的。

任何帮助将非常感激。

对于正在编译的文件或整个项目,ARC显然是closures的。 静态分析器只会在ARCclosures时抱怨这个问题,所以我会进一步阐述这一点。

编译器引用的Cocoa内存pipe理准则只允许一组严格的方法在正常情况下(即'init','copy','mutableCopy'和'new')从构造函数返回非自动释放对象。 注意模式?

因为你在便利构造函数中分配了一个对象,然后把它交给调用者,你就是拥有它的人,因为你创build了它。 Cocoa希望你做的是在返回的末尾添加一个autorelease,以便调用者保持对新构造对象的引用:

 + (id<MXURLRequest>) requestWithURL:(NSURL*)url { MXASIURLRequest *request = [[MXASIURLRequest alloc] init]; +1, we own it [request setUrl:url]; return [request autorelease]; // +0, it's the caller's problem now } 

至于最后一种方法,Cocoa抱怨你是不符合记忆的。 该方法有可能返回一个+1,或+0保留计数的对象,因为你的构造函数和方便的混合使用。 为了显示:

 - (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]; //returns +0 } else if ([contentType hasPrefix:@"image/"] || [contentType hasPrefix:@"audio/"] || [contentType hasPrefix:@"application/octet-stream"]) { body = [_request responseData]; //potentially returns +1 } else { body = [[NSString alloc] initWithData:[_request responseData] encoding:NSUTF8StringEncoding]; //returns +1 } return body; // STATIC ANALYSER : Potential leak of an object stored into 'body' } 

分析器要求一致性:要么只使用构造函数,要么始终使用自动释放。