iOS开发之Xcode的静态分析(Static Code Analysis)与常见内存隐患解决办法
来源:互联网 发布:python idle打开文件 编辑:程序博客网 时间:2024/04/29 11:05
一.Xcode Analyze静态分析 Static Code Analysis
Static Code Analysis(静态代码分析)用来发现源代码潜在的错误与缺陷,源代码编译后只有在运行时有可能会产生细微的错误,他们可能难以识别和修复,所以这些潜在的威胁在开发过程中一定要尽可能清理干净,尽量编写的时候注意类似可能潜在的威胁与缺陷
(点击查看苹果Static Code Analysis官方介绍)
二.可以识别的问题与缺陷
1.逻辑缺陷,如访问未初始化的变量和空指针
2.内存管理的缺陷,如分配后的内存泄漏
3.无效数据(未使用的变量)缺陷
4.使用api缺陷,不遵循库或者框架的使用规则
三.打开方式
1.Xcode顶部菜单Product-->Analyze即可开启
2.长按Xcode的run运行箭头选择Analyze即可开启
3.cmd + shift + b 快捷键开启
四.使用方法
1.左侧问题导航选择某一条蓝色警告分析
2.点击右侧源码编辑器的蓝色消息体
3.查看错误跟踪线
4.编辑代码修改错误
五.常见问题缺陷与解决办法
1.Passed-by-value struct argument contains uninitialized data (e.g., via the field chain: 'origin.x')
错误:
正确:
2.Value stored to 'array' during its initialization is never read
变量申请了内存并初始化,但没有使用此变量,接着将此变量又重新赋值
错误:
正确:
3.Value stored to 'array' is never read
变量'array' 未被使用
4. The 'viewWillAppear:' instance method in UIViewController subclass 'TempViewController' is missing a [super viewWillAppear:] call
错误
正确
5.Assigned value is garbage or undefined
变量没有初始化就赋值给其他变量时会出现这个提示
6.Access to instance variable 'viewDelegate' results in a dereference of a null pointer (loaded from variable 'self')
如果self是nil,那么你不能访问self的实例变量 。所以应该引用这些变量在if self语句中,比如init方法中 当self不为nil方可使用
7.Potential leak of an object stored into 'imageRef'
CGImageRelease(imageRef)
CG开头的这些C-types对象使用过后都需要手动Release
8.Potential leak of an object
潜在的内存泄漏问题
9.Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected
10.Instance variable used while 'self' is not set to the result of '[(super or self) init...]'
错误
正确
11.The left operand of '+' is a garbage value /The left operand of '-' is a garbage value
数据赋值隐患
12.Instance variable 'program' in class 'PLRenderer' is never used by the methods in its @implementation (although it may be used by category methods)
13.Key argument to 'setObject:forKey:' cannot be nil/Value argument to 'setObject:forKey:' cannot be nil
key或者value不能为nil
14.Incorrect decrement of the reference count of an object that is not owned at this point by the caller
变量申请了内存并初始化,但没有使用此变量,接着将此变量又重新赋值
15.Array access (from variable 's') results in a null pointer dereference
16.Dereference of null pointer
空指针引用
17.Argument in message expression is an uninitialized value
18.Function call argument is an uninitialized value
变量未进行初始化后被其他变量引用
错误
正确
19.Division by zero
错误
正确 加个非零判断
20.Receiver in message expression is a garbage value
要回收的变量接受到了消息
21.Argument to free() is offset by 2 bytes from the start of memory allocated by malloc()
22.Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil
参数可能为nil
- iOS开发之Xcode的静态分析(Static Code Analysis)与常见内存隐患解决办法
- iOS开发之Xcode的静态分析(Static Code Analysis)与常见内存隐患解决办法
- iOS开发之Xcode的静态分析(Static Code Analysis)与常见问题解决
- iOS开发之Xcode的静态分析(Static Code Analysis)与常见问题解决
- iOS开发 - 内存静态分析
- Xcode 静态分析与内存泄漏检查
- iOS开发 Xcode使用Analyze静态分析
- iOS开发中(Xcode)常见报错和解决办法
- static code analysis
- static code analysis
- iOS开发-xcode无法提交.a静态库文件的解决办法
- IOS开发之-XCODE疯狂吃内存
- iOS - 静态与动态内存分析
- iOS开发——Xcode Analyze静态分析
- iOS Xcode开发技巧之Code Snippets Library
- iOS开发中Xcode编译错误 linker command failed with exit code 1 (use -v to see invocation) 的解决办法
- static关键字的使用与内存分析
- In-Depth: Static Code Analysis
- Android中的基础组件
- android第二节
- 使用缓存Memcache存储更新微信access token
- 00后《新白娘子传奇》热度爆棚,最佳翻拍不输雅芝姐
- 深度学习常用的Toolkit
- iOS开发之Xcode的静态分析(Static Code Analysis)与常见内存隐患解决办法
- 11.2.3、搭建RESTFUL API 之 带过滤器的WSGI服务
- Button上的文字显示为大写,已解决
- [读书笔记] 深入探索C++对象模型-第六章-执行期语义学(下)
- 1.使用ApplicationContext的方式实例化Spring容器。
- 设计模式学习(二十七)————解释器模式
- JAVA类加载机制基础
- 关于嵌套滑动 CoordinatorLayout 学习
- ssh免密登陆脚本