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

时间:2013-02-13 16:32:03

标签: ios objective-c memory-management automatic-ref-counting

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:7)

对于要编译的文件或整个项目,ARC显然已关闭。当ARC关闭时,静态分析仪只会抱怨这些东西,所以我会仔细考虑这一点。

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

因为您在便捷构造函数中分配对象,然后将其交给调用者,所以您拥有该对象,因为您创建了它。 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'
}

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