Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Xcode静态分析仪抱怨使用ARC时存在潜在泄漏_Ios_Objective C_Memory Management_Automatic Ref Counting - Fatal编程技术网

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

Ios Xcode静态分析仪抱怨使用ARC时存在潜在泄漏,ios,objective-c,memory-management,automatic-ref-counting,Ios,Objective C,Memory Management,Automatic Ref Counting,我正在使用带ios sdk 6.0的ARC 我很确定我有一些内存泄漏,我很难找到 运行静态分析仪后,我收到关于以下两种方法的警告: + (id<MXURLRequest>) requestWithURL:(NSURL*)url { MXASIURLRequest *request = [[MXASIURLRequest alloc] init]; [request setUrl:url]; return request; // STATIC ANALYSER:

我正在使用带ios sdk 6.0的ARC

我很确定我有一些内存泄漏,我很难找到

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

+ (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'
}
有人能看出我做错了什么吗?这些警告是合法的,还是可以忽略?在我看来,这些方法对于ARC能够处理自动参考计数是有效的


任何帮助都将不胜感激

对于要编译到的文件或整个项目,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
}

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

您确定要使用ARC吗?将
[请求释放]
[正文释放]
放在那里,查看它是否编译。在
返回请求
行中附加一个
-autorelease
。我敢打赌你没有使用ARC。@WDUK Im def使用ARC,如果我添加发布调用,它将无法编译。@CodaFi你的意思是自动删除吗?@Sabobin否。自动删除通常是为管理双指针而保留的。非常感谢你,你是对的,我错误地在该文件上关闭了ARC。为该文件重新启用ARC可使警告静音+1@Sabobin没问题。让我举一个关于第二个函数抛出内存警告的最后编辑,仅仅是为了学究的缘故。
+ (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
}
- (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'
}