循环引用不释放控制器
理论不介绍,直接上干货~
一、检测
检测控制器是否被释放,可以检测是否调用了-dealloc生命周期方法。
二、修复
1、定时器未停止
1.1 因为NSTimer没有释放的原因
避免方式:记得将NSTimer对象timerObj失效与置空。
[self.timerObj invalidate];
self.timerObj = nil;
1.2 NSObject的performSelector延迟执行
避免方式:调要后要记得取消performSelector
@interface NSObject (NSDelayedPerforming) - (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSRunLoopMode> *)modes; - (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay; + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument; + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget; @end @interface NSRunLoop (NSOrderedPerform) - (void)performSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg order:(NSUInteger)order modes:(NSArray<NSRunLoopMode> *)modes; - (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg; - (void)cancelPerformSelectorsWithTarget:(id)target; @end
2、成为别人的委托对象,却把自己给搭进去出不来
避免方式:声明delegate时请用assign(MRC)或者weak(ARC)。
3、block环
避免方式:
【1】如果是属性
@property (nonatomic, assign) NSInteger *age;
在block外面:__weak typeof(self) weakSelf = self;
在block里面:weakSelf.age = 18;
【2】如果是成员变量
{ NSString *_name; }
在block外面:__weak typeof(self) weakSelf = self;
在block里面:__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf->_name = @“Ming”;