-
Block循環(huán)引用
self持有Block鬓催,Block里又持有self,造成了一個引用環(huán)赏僧,最終導(dǎo)致self無法釋放庭敦。
// weakself弱引用不會讓block持有self __weak typeof(self) weakself = self; [self.label ly_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) { // strongself是為了防止weakself提前釋放 __strong typeof(self) strongself = weakself; [self dismissViewControllerAnimated:YES completion:nil]; }];
-
Delegate循環(huán)引用問題
self持有delegate的擁有者,delegate擁有者通過strong持有self兰迫,造成循環(huán)引用户秤。
@property (nonatomic, weak) id delegate;
-
NSTimer循環(huán)引用
NSTimer會造成循環(huán)引用,timer會強引用target即self逮矛,一般self又會持有timer作為屬性鸡号,這樣就造成了循環(huán)引用。
// 1.Block弱引用破除循環(huán)引用(https://www.cnblogs.com/H7N9/p/6540578.html) weak_self [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) { [weak_self dosomething]; }]; // 2.NSProxy消息轉(zhuǎn)發(fā)實現(xiàn)破除循環(huán)引用(http://www.reibang.com/p/1ef002fcf314) // proxy子類weak的target须鼎,然后實現(xiàn)target的消息轉(zhuǎn)發(fā)鲸伴。 // 初始化proxy子類 self.proxy = [WBProxy alloc]; // 作為當(dāng)前控制器的代理 self.proxy.obj = self; // target為代理 self.timer = [NSTimer timerWithTimeInterval:1 target:self.proxy selector:@selector(timerEvent) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
-
非OC對象內(nèi)存處理
注意以creat,copy作為關(guān)鍵字的函數(shù)都是需要釋放內(nèi)存的,注意配對使用晋控。比如:CGColorCreate<-->CGColorRelease
CIImage *beginImage = [[CIImage alloc]initWithImage:[UIImage imageNamed:@"yourname.jpg"]]; ... CGImageRelease(ref);//非OC對象需要手動內(nèi)存釋放