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.lastDataSourceArr和self.hotDataSourceArr都是已經初始化過的可變數組,
但是在if里我又重新初始化了一個新的可變數組,并且把之前已經初始化過的可變數組賦值給了它,
這就是內訓泄漏的問題所在,看似代碼沒有大的問題,但是其實已經造成了內存泄漏。
原因是:因為self.lastDataSourceArr和self.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]){
[selfsetView];
}
returnself;
}
解決辦法:
先調用父類的初始化方法,再自定義處理
-(instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
[selfsetView];
}
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
自己定義的方法:
系統的方法:
解決方式:命名方式不要和系統方法重名,改一下自己定義方法的名字。
寫的都是我發現和整理的問題,歡迎大家來繼續補全。