XCode的Analyze可以分析到項(xiàng)目哪里有內(nèi)存泄露.
方法:xcode----product-----Analyze(快捷鍵:Shift + Cmd + B)
****iOS的分析工具可以發(fā)現(xiàn)編譯中的warning,內(nèi)存泄漏隱患,甚至還可以檢查出logic上的問題;所以在自測階段一定要解決Analyze發(fā)現(xiàn)的問題,可以避免出現(xiàn)嚴(yán)重的bug;****
常見問題
1.內(nèi)存泄漏隱患提示:Potential Leak of an object allocated on line ……
****2.數(shù)據(jù)賦值隱患提示:The left operand of …… is a garbage value;
3.對象引用隱患提示:Reference-Counted object is used after it is released;
以上提示均比較嚴(yán)重,可能會引起嚴(yán)重問題,需要開發(fā)者密切關(guān)注!
1、檢測內(nèi)存泄漏時出現(xiàn)泄漏提示:User-facing text should use localized string macro。// 面向用戶的文本應(yīng)該使用本地化的字符串宏
.user-facingtextshould use localizedstringmacro。
此為代碼中配置了本地化,面向用戶的應(yīng)該用字符串宏,而我們直接賦值為漢字,因此,此提示可以忽略.
解決辦法:
2、重寫UIViewController的生命周期方法沒有調(diào)用父類的方法
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;
}
仔細(xì)看了代碼后才發(fā)現(xiàn)代碼存在一個細(xì)節(jié)上的問題,也正是這個細(xì)節(jié)導(dǎo)致的內(nèi)存泄漏。
在項(xiàng)目里我的self.lastDataSourceArr和self.hotDataSourceArr都是已經(jīng)初始化過的可變數(shù)組,
但是在if里我又重新初始化了一個新的可變數(shù)組,并且把之前已經(jīng)初始化過的可變數(shù)組賦值給了它,
這就是內(nèi)訓(xùn)泄漏的問題所在,看似代碼沒有大的問題,但是其實(shí)已經(jīng)造成了內(nèi)存泄漏。
原因是:因?yàn)閟elf.lastDataSourceArr和self.hotDataSourceArr是已經(jīng)創(chuàng)建并且分配過內(nèi)存的可變數(shù)組了,但是我把這些數(shù)組又賦值給了重新創(chuàng)建并分配了內(nèi)存的可變數(shù)組tempMutArr,所以這樣就出現(xiàn)了一個數(shù)據(jù)源卻申
請了兩塊內(nèi)存的情況,那么就存在一塊內(nèi)存空閑了,所以就存在了內(nèi)存泄漏。
解決辦法:
無用的初始化直接去掉!
NSMutableArray *tempMutArr;
if ([self.clickedButtonTpye isEqualToString:KClickedButtonTypeLast]) {
tempMutArr = self.lastDataSourceArr;
}else{
tempMutArr = self.hotDataSourceArr;
}
這樣只做了一個可變數(shù)組tempMutArr的聲明,不需要給它分配實(shí)際內(nèi)存,就可以賦值了,
其實(shí)只是聲明了這樣一個對象,使其持有對原有數(shù)據(jù)對象的引用即可。
5、重新父類的初始化方法時沒有調(diào)用父類的初始化方法
錯誤代碼示例:
-(instancetype)initWithFrame:(CGRect)frame{
if(self== [superinitWithFrame:frame]){
[selfsetView];
}
returnself;
}
解決辦法:
先調(diào)用父類的初始化方法,再自定義處理
-(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);
解決方式:初始化數(shù)據(jù)。NSInteger ret = 1;
7、自定義的方法名和系統(tǒng)的方法名重名,且返回的類型不一樣:
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
自己定義的方法:
系統(tǒng)的方法:
解決方式:命名方式不要和系統(tǒng)方法重名,改一下自己定義方法的名字。
寫的都是我發(fā)現(xiàn)和整理的問題,歡迎大家來繼續(xù)補(bǔ)全。