Analyze 静态分析

1、Analyze

  • 使用 Xcode 自带的静态分析工具 Product -> Analyze(快捷键 command + shift + B)可以找出代码潜在错误,如内存泄露,未使用函数和变量等。

  • Analyze 主要分析以下四种问题:

    • 1、逻辑错误:访问空指针或未初始化的变量等;
    • 2、内存管理错误:如内存泄漏等,比如 ARC 下,内存管理不包括 core foundation;
    • 3、声明错误:从未使用过的变量;
    • 4、Api 调用错误:未包含使用的库和框架。
  • 官方文档 Xcode 执行静态代码分析视频教程

2、分析结果处理

  • 1、user-facing text should use localized string macro

    • 面向用户的文本应该使用本地化的字符串宏。此为代码中配置了本地化,面向用户的应该用字符串宏,而我们直接赋值为汉字,因此此提示可以忽略。
  • 2、instance variable used while ‘self’ is not set to the result of ‘[(super or self) init…]

    1
    2
    3
    4
    5
    6
    7
    8
    // 此方法提示错误
    - (instancetype)initWithType:(FTFFavorateType)type {

    if (self == [super init]) {
    _type = type;
    }
    return self;
    }
    • 修改为如下

      1
      2
      3
      4
      5
      6
      7
      - (instancetype)initWithType:(FTFFavorateType)type {

      if (self = [super init]) {
      _type = type;
      }
      return self;
      }
  • 3、Value stored to ‘durationValue’ during its initialization is never read

    • 在初始化过程中存储的 “持续时间值” 的值永远不会被读取

      1
      2
      3
      // 此段代码提示错误
      NSMutableArray *datesArray = [[NSMutableArray alloc] init];
      datesArray = [_onDemandDictionary objectForKey:key];
    • 这是因为 [NSMutableArray alloc] init] 初始化分配了内存,而判断语句里面 [_onDemandDictionary objectForKey:key] 方法也相当于初始化分配了内存,就是把初始化的一个新的可变数组赋值给之前已经初始化过的可变数组,看似没什么大问题,其实存在一个数据源却申请了两块内存的问题,已经造成了内存泄露。

    • 修改为如下

      1
      2
      NSMutableArray *datesArray = nil;
      datesArray = [_onDemandDictionary objectForKey:key];
  • 4、Potential leak of an object stored into ‘imageRef’

    • imageRef 对象有内存泄漏

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      + (UIImage*)getSubImage:(unsigned long)ulUserHeader {

      UIImage * sourceImage = [UIImage imageNamed:@"header.png"];
      CGFloat height = sourceImage.size.height;
      CGRect rect = CGRectMake(0 + ulUserHeader*height, 0, height, height);

      CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], rect);
      UIImage* smallImage = [UIImage imageWithCGImage:imageRef];
      // CGImageRelease(imageRef);

      return smallImage;
      }
  • 5、Analyze 逻辑错误监测

    • 这种情况在 codereview 时也较难发现,可以借助 Analyze。
    • 如上代码,当 Tag 不等于 1、2 和 3 的时候,就会出现很问题了。
    • Analyze 还给出了箭头提示:len is a garbage value。建议在声明变量时,同时进行初始化。

3、内存分析

3.1 静态内存分析

  • 所谓静态内存分析,是指在程序没运行的时候,通过 Xcode 自带的静态分析工具 Product -> Analyze(快捷键 command + shift + B)对代码直接进行分析。根据代码的上下文的语法结构,让编译器分析内存情况,检查是否有内存泄露。

  • 缺点:静态内存分析由于是编译器根据代码进行的判断, 做出的判断不一定会准确, 因此如果遇到提示, 应该去结合代码上文检查一下。

  • 内存泄漏提示:Potential leak of an object stored into ‘imageRef’

    • imageRef 对象有内存泄漏

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      + (UIImage*)getSubImage:(unsigned long)ulUserHeader {

      UIImage * sourceImage = [UIImage imageNamed:@"header.png"];
      CGFloat height = sourceImage.size.height;
      CGRect rect = CGRectMake(0 + ulUserHeader*height, 0, height, height);

      CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], rect);
      UIImage* smallImage = [UIImage imageWithCGImage:imageRef];
      // CGImageRelease(imageRef);

      return smallImage;
      }

3.2 动态内存分析

  • 动态内存分析通过 Xcode 自带的动态分析工具 Xcode -> Product -> Profile(Leaks 工具)动态的对内存进行分析,大多时候只是堆内存的分析。

3.3 动态加载图片的内存分析

  • imageNamed 和 imageWithContentOfFile 方法的比较。

  • 1、imageName 加载图片

    • a、当 imageview 对象销毁时候,图片对象不会随着一起销毁。

    • b、加载的图片占据的内存比较大。

    • c、相同的图片只会加载一份到内存中,如果同时使用,使用的是同一个图片对象。

  • 2、imageWithContentOfFile 加载图片

    • a、当 imageView 对象销毁的时候,图片对象会随着一起销毁。

    • b、加载的图片占用的内存比较小。

    • c、相同的图片对象会多次加载到内存中,如果同时使用图片,使用的是不同的对象。

  • 总结

    • imageName:如果一些图片在多个界面都会使用,并且图片较小,使用频率高,(图标/小的背景图)。

    • imageWithContentOfFile:只在一个地方使用,并且图片比较大,使用频率不高,(相册/版本新特性)。

文章目录
  1. 1. 1、Analyze
  2. 2. 2、分析结果处理
  3. 3. 3、内存分析
    1. 3.1. 3.1 静态内存分析
    2. 3.2. 3.2 动态内存分析
    3. 3.3. 3.3 动态加载图片的内存分析
隐藏目录