iOS内存检测工具Analyze的使用

来源:互联网 发布:物理地址是mac地址吗 编辑:程序博客网 时间:2024/05/29 12:32

XCode的Analyze可以分析到项目哪里有内存泄露.

  方法:xcode----product-----Analyze(快捷键:Shift + Cmd + B)iOS的分析工具可以发现编译中的warning,内存泄漏隐患,甚至还可以检查出logic上的问题;所以在自测阶段一定要解决Analyze发现的问题,可以避免出现严重的bug;常见问题1.内存泄漏隐患提示:Potential Leak of an object allocated on line …2.数据赋值隐患提示:The left operand of …… is a garbage value;3.对象引用隐患提示:Reference-Counted object is used after it is released;以上提示均比较严重,可能会引起严重问题,需要开发者密切关注!

1、检测内存泄漏时出现泄漏提示:User-facing text should use localized string macro。
// 面向用户的文本应该使用本地化的字符串宏
.user-facingtextshould use localizedstringmacro。
这里写图片描述
此为代码中配置了本地化,面向用户的应该用字符串宏,而我们直接赋值为汉字,因此,此提示可以忽略.
解决办法:
这里写图片描述
2、重写UIViewController的生命周期方法没有调用父类的方法

 The 'viewWillDisappear:' instance method in UIViewController subclass 'TestDetailsViewController' is missing a [super viewWillDisappear:] call-(void)viewWillDisappear:(BOOL)animated{}解决方案:-(void)viewWillDisappear:(BOOL)animated{        [super viewWillDisappear:animated];}

3、初始化的变量并没有被使用

提示示例:value stored to ‘YourVariable’ is never read解决办法:如果无用的话,删除或者注释即可!如果有用的话,检查为何没有被使用!

4、变量多次初始化,其中的某些初始化的变量并没有使用过

提示示例:value stored to ‘YourVariable’during its initialization is never read错误代码示例:NSMutableArray *tempMutArr = [NSMutableArray arrayWithCapacity:0];    if ([self.clickedButtonTpye isEqualToString:KClickedButtonTypeLast]) {        tempMutArr = self.lastDataSourceArr;    }else{        tempMutArr = self.hotDataSourceArr;    }仔细看了代码后才发现代码存在一个细节上的问题,也正是这个细节导致的内存泄漏。在项目里我的self.lastDataSourceArrself.hotDataSourceArr都是已经初始化过的可变数组,但是在if里我又重新初始化了一个新的可变数组,并且把之前已经初始化过的可变数组赋值给了它,这就是内训泄漏的问题所在,看似代码没有大的问题,但是其实已经造成了内存泄漏原因是:因为self.lastDataSourceArrself.hotDataSourceArr是已经创建并且分配过内存的可变数组了,但是我把这些数组又赋值给了重新创建并分配了内存的可变数组tempMutArr,所以这样就出现了一个数据源却申请了两块内存的情况,那么就存在一块内存空闲了,所以就存在了内存泄漏。

解决办法:

无用的初始化直接去掉!NSMutableArray *tempMutArr;    if ([self.clickedButtonTpye isEqualToString:KClickedButtonTypeLast]) {        tempMutArr = self.lastDataSourceArr;    }else{        tempMutArr = self.hotDataSourceArr;这样只做了一个可变数组tempMutArr的声明,不需要给它分配实际内存,就可以赋值了,其实只是声明了这样一个对象,使其持有对原有数据对象的引用即可。

5、重新父类的初始化方法时没有调用父类的初始化方法

错误代码示例:

-(instancetype)initWithFrame:(CGRect)frame{if(self== [superinitWithFrame:frame]){    [self setView];}returnself;}

解决办法:
先调用父类的初始化方法,再自定义处理

-(instancetype)initWithFrame:(CGRect)frame{self = [superinitWithFrame:frame];if (self) {    [self setView];}returnself;}

6、变量未初始化就使用了
错误信息:The left expression of the compound assignment is an uninitialized value. The computed value will also be garbag
提示示例:The left operand of ‘+’ is a garbage value
错误示例;

   NSInteger ret;    ret += 2;    NSLog(@"%ld",ret);
解决方式:初始化数据。NSInteger ret = 1;

7、自定义的方法名和系统的方法名重名,且返回的类型不一样:
The Objective-C class ‘LZBKeyBoardToolBar’, which is derived from class ‘UIResponder’, defines the instance method ‘becomeFirstResponder’ whose return type is ‘void’. A method with the same name (same selector) is also defined in class ‘UIResponder’and has a return type of ‘BOOL’. These two types are incompatible, and may result in undefined behavior for clients of these classes
自己定义的方法:
这里写图片描述
系统的方法:
这里写图片描述
解决方式:命名方式不要和系统方法重名,改一下自己定义方法的名字。
写的都是我发现和整理的问题,欢迎大家来继续补全。

原创粉丝点击