Xcode静态分析器抱怨使用ARC时可能发生泄漏 [英] Xcode static analyser complaining about potential leak when using ARC

查看:103
本文介绍了Xcode静态分析器抱怨使用ARC时可能发生泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ARC与ios sdk 6.0一起使用.

I am using ARC with ios sdk 6.0.

我很确定我有一些内存泄漏,我无法追踪到.

I am pretty sure I have got some memory leaks which Im having trouble tracking down.

运行静态分析器后,我会收到有关以下两种方法的警告:

After running the static analyser, im getting warnings about the following two methods:

+ (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能够处理自动引用计数似乎是有效的.

Can anyone see what I've done wrong here? Are these warnings legitimate, or can they be ignored? To me these methods look valid for ARC to be able to handle automatic reference counting.

任何帮助将不胜感激.

Any help would be much appreciated.

推荐答案

对于要编译到该文件的文件或整个项目,ARC显然已关闭.静态分析器仅在ARC关闭时才抱怨这些东西,因此我将在此作进一步说明.

ARC is clearly turned off either for the file that this is compiling down to, or on the whole project. The static analyzer only complains about this stuff when ARC is turned off, so I'll elaborate further with that in mind.

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

The Cocoa Memory Management Guidelines that the compiler references allow only a strict set of methods to return non-autoreleased objects from constructors under normal circumstances (those being 'init', 'copy', 'mutableCopy', and 'new'). Notice the pattern?

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

Because you are allocating an object in the convenience constructor, then handing it to the caller, you are the one that owns it, because you created it. What Cocoa wants you to do is append an autorelease to the end of that return so that it is now the caller's job to keep a reference to the newly constructed object:

+ (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保留计数的对象.为了说明:

As for the last method, Cocoa is complaining about you being inconsistent memory-wise. That method has the possibility of returning either an object with a +1, or +0 retain count because of your mixed use of constructors and conveniences. To illustrate:

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

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

The analyzer is asking for consistency: Either use only constructors, or autorelease consistently.

这篇关于Xcode静态分析器抱怨使用ARC时可能发生泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆