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

2019-10-17 17:17发布

我使用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,以便能够处理自动引用计数。

任何帮助将非常感激。

Answer 1:

ARC显然是关闭无论是对,这是编译下跌,或对整个项目中的文件。 静态分析只抱怨这个东西时,ARC被关闭,所以我会考虑到这一点进一步阐述。

可可内存管理指南,编译器引用只允许一套严格的方法,从正常情况下的构造函数返回非自动释放的对象(是的那些“初始化”,“复制”,“mutableCopy”和“新”)。 注意模式?

因为你分配的便捷构造一个对象,然后递给调用者,您是拥有它的人,因为你创造了它。 什么可可要你做的是添加一个自动释放到回复结束,以便它现在是调用者的工作是让新构造的对象的引用:

+ (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
}

至于最后一种方法,可可抱怨你是不一致的内存明智的。 这种方法有,因为你混合使用的构造函数和便利的返回要么用+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'
}

该分析仪要求的一致性:要么只使用构造函数,或持续自动释放。



文章来源: Xcode static analyser complaining about potential leak when using ARC