要說發(fā)現(xiàn)問題 從程序crash開始 翻斟,程序有時(shí)會(huì)發(fā)生系統(tǒng)捕捉不到的信號(hào)異常。
比較常見的是:
Signal 11 was raised. SIGSEGV
Signal 6 was raised. SIGABRT
這兩個(gè)異常一個(gè)是釋放已釋放的內(nèi)存趋箩,一個(gè)是使用了無(wú)效內(nèi)存∨吕纾看過這兩個(gè)問題的解釋后哨苛,我就開始覺得是內(nèi)存泄漏的問題。
首先第一步篮赢,我使用最直白的方法
- (void)dealloc{
NSLog(@"%@---dealloc",[self class]);
}
dealloc打印 在ViewController的基類里加入以上打印齿椅,這樣vc釋放的時(shí)候就能夠看到了,對(duì)項(xiàng)目進(jìn)行檢查启泣,看那個(gè)Controller釋放的時(shí)候沒有打印涣脚,就可以鎖定到哪些Controller有問題。再對(duì)Controller進(jìn)行逐一檢查寥茫,鎖定問題遣蚀。常見的問題有
問題一:
@interface HeadImgPickerView :UIView
@property(nonatomic,strong)UIViewController * controller;
@property (nonatomic, strong) id delegate;
- (instancetype)initWithViewController:(UIViewController *)vc;
-(void)show;
@end
delegate使用強(qiáng)引用,這里的delegate坠敷,controller都用該使用weak不然就會(huì)循環(huán)引用妙同。
問題二:block 導(dǎo)致的計(jì)數(shù)+1無(wú)法回收
[alert addButton:Button_OTHER withTitle:@"相冊(cè)" handler:^(RoadAlertDialogItem *item) {
? ?[self dosomething];
}];
block里引用block外的變量一定要進(jìn)行weak處理,通常方式block內(nèi)丟失還會(huì)在內(nèi)部strong處理一下膝迎。
@WeakObj(self)
[alert addButton:Button_OTHER withTitle: @"相冊(cè)" handler:^(RoadAlertDialogItem *item) {
@StrongObj(self)
[self dosomething];
}];
注:這里附上我的兩個(gè)宏
#define WeakObj(o) try{}@finally{} __weak typeof(o) o##Weak = o;
#define StrongObj(o) try{}@finally{}? __strong typeof(o) o = o##Weak;
通過排查粥帚,處理了這兩類問題基本上表面上的泄漏就差不多了。
下面使用xcode自帶的工具檢測(cè)一下限次,我就不再細(xì)說了芒涡。見推薦閱讀