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

Sab*_*bin 1 memory-management objective-c ios 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'
}
Run Code Online (Sandbox Code Playgroud)

警告如下......

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
Run Code Online (Sandbox Code Playgroud)

谁能看到我在这里做错了什么?这些警告是合法的,还是可以被忽略?对我来说,这些方法看起来对ARC有效,能够处理自动引用计数.

任何帮助将非常感激.

Cod*_*aFi 7

显然,对于要编译的文件或整个项目,ARC都处于关闭状态.当ARC关闭时,静态分析仪只会抱怨这个东西,所以我会进一步详细说明.

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

因为您在便捷构造函数中分配对象,然后将其交给调用者,所以您拥有该对象,因为您创建了它.Cocoa要求你做的是在返回的末尾添加一个自动释放,以便现在调用者的工作是保持对新构造的对象的引用:

+ (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
}
Run Code Online (Sandbox Code Playgroud)

至于最后一种方法,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'
}
Run Code Online (Sandbox Code Playgroud)

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

  • 非常感谢你,你是对的,我错误地关闭了该档案的弧线.为该文件重新打开ARC会使警告静音.+1 (2认同)