循环引用
循环引用,顾名思义就是多个对象之间相互引用,ARC(Automatic Reference Counting)机制下很容易产生循环引用,在这种情况下会导致内存泄露,当然这是我们作为开发者所不能允许的,所以我们要避免这种情况的发生。
循环引用会在我们的开发中经常涉及到,今天我们主要看下delegate和block中什么样的情况不会出现循环引用,又有什么样的情况会出现循环引用以及如何解决。
代码说明
用Objective-C和Swift写的两个小Demo来说明。后面会给出下载地址。
Demo是以一个UINavigationController控制界面的跳转,界面的流程是:ViewController->OneViewController->TwoViewController或者ThreeViewController
App Flow
delegate
1 分别在OneViewController和TwoViewController中重写了delloc方法,在他们的对象销毁时会在控制台打印出Log:
- (void)dealloc {NSLog(@"OneViewController dealloc");}
- (void)dealloc {NSLog(@"TwoViewController dealloc");}
2OneViewController中声明一个全局变量twoController:
@interfaceOneViewController(){ TwoViewController *twoController;}
3TwoViewController中定义了一个delegate:
@property(strong,nonatomic)id delegate;
4OneViewController中Delegate Circular Ref按钮链接的函数是:
- (IBAction)delegateCircularRefButtonClick { twoController = [TwoViewController new]; twoController.delegate =self; [self.navigationController pushViewController:twoController animated:YES];}
点击Delegate Circular Ref按钮Push到TwoViewController后,然后依次点击返回按钮Pop到ViewController,会发现控制台并没有打印出任何Log。这说明OneViewController和TwoViewController的对象并没有释放内存,因为这里出现了循环引用。
self -> twoController -> delegate -> self
这就是造成对象不会释放的原因,对象直接互相引用着,导致了内存泄露。我们接着往下看:
5OneViewController中Delegate No Circular Ref按钮链接的函数是:
- (IBAction)delegateNoCircularRefButtonClick {` TwoViewController *controller = [TwoViewController new]; controller.delegate =self; [self.navigationController pushViewController:controller animated:YES];}
点击Delegate No Circular Ref按钮Push到TwoViewController后,然后点击返回按钮Pop到OneViewController,会发现控制台打印出Log:
TwoViewController dealloc
再次点击返回按钮Pop到ViewController,会发现控制台打印出Log:
OneViewController dealloc
这说明OneViewController和TwoViewController的对象销毁释放内存,这里没有出现循环引用。
controller -> delegate -> self
两段代码比较,很容易发现哪里出现了循环引用,第一段代码中self持有了twoController,造成了一个引用环。
重要说明:不要因为第二种情况不会出现循环引用就可以在工程中对delegate使用strong属性,还是要使用weak属性!正确的代码是:
@property(weak,nonatomic)id delegate;
block
1ThreeViewController中重写了delloc方法,在对象销毁时会在控制台打印出Log:
- (void)dealloc {NSLog(@"ThreeViewController dealloc");}
2OneViewController中声明一个全局变量threeController:
@interfaceOneViewController(){ ThreeViewController *threeController;}
OneViewController中定义了一个实例变量:
@property(strong,nonatomic)NSString*name;
OneViewController在viewDidLoad中对实例变量赋值:
self.name =@"Paolo Maldini";`
3ThreeViewController中定义了一个block:
typedefvoid(^BlockTest) ();@interfaceThreeViewController:UIViewController@property(copy,nonatomic) BlockTest block;@end
4ThreeViewController在viewDidDisappear中执行block:
_block();
5OneViewController中Block Circular Ref按钮链接的函数是:
- (IBAction)blockCircularRefButtonClick { threeController = [ThreeViewController new]; threeController.block = ^() {NSLog(@"Hello, %@!",self.name); }; [self.navigationController pushViewController:threeController animated:YES];}
Xcode 已经提示 “Capturing 'self' strongly in this block is likely to lead to a retain cycle”
点击Block Circular Ref按钮Push到ThreeViewController后,然后依次点击返回按钮Pop到ViewController,会发现控制台只打印出block里的内容
Hello, Paolo Maldini!
这说明OneViewController和ThreeViewController的对象并没有释放内存,因为这里出现了循环引用。
self -> threeController -> block -> self
这就是造成对象不会释放的原因,对象直接互相引用着,导致了内存泄露。我们接着往下看:
6OneViewController中Block No Circular Ref按钮链接的函数是:
- (IBAction)blockNoCircularRefButtonClick { ThreeViewController *controller = [ThreeViewController new]; controller.block = ^() {NSLog(@"Hello, %@!",self.name); }; [self.navigationController pushViewController:controller animated:YES];}
点击Block No Circular Ref按钮Push到ThreeViewController后,然后点击返回按钮Pop到OneViewController,会发现控制台打印出Log:
Hello, Paolo Maldini!
ThreeViewController dealloc
再次点击返回按钮Pop到ViewController,会发现控制台打印出Log:
OneViewController dealloc
这说明OneViewController和ThreeViewController的对象销毁释放内存,这里没有出现循环引用。
controller -> block -> self
两段代码比较,很容易发现哪里出现了循环引用,第一段代码中self持有了threeController,造成了一个引用环。修改方法,用 *__weak *声明一个弱引用对象:
- (IBAction)blockCircularRefButtonClick { threeController = [ThreeViewController new]; __weakOneViewController *weakSelf =self; threeController.block = ^() {NSLog(@"Hello, %@!", weakSelf.name); }; [self.navigationController pushViewController:threeController animated:YES];}
作者:iLB
链接:https://www.jianshu.com/p/22f27458d25a
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。