年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2237|回复: 0

ARC如何解决循环相互引用的问题

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-20 11:31:53 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2014-3-28 23:29 编辑

    1、在MRC中,相互引用问题是在其中一方的成员变量属性retain修改成assign,参考:http://www.yusian.com/forum.php?mod=viewthread&tid=948&fromuid=3
    2、在ARC中,同样的原理,将其中一方成员变量属性中的strong修改成weak;
    3、图示说明一下,首先创建两个对象

    001.PNG


    4、两个对象中各有一成员变量相互指向对方,现将Person对象中的_dog设置为强引用,即_dog为强指针,Dog对象中的_person为弱指针:

    002.png


    5、当main函数结束时,Person * p与Dog * d会被释放:

    003.png


    6、由于Person对象只有一个弱指针(Dog对象中的_person)指向该对象,ARC机制将其释放:

    005.png


    7、Person对象被释放,对象成员变量_dog也随一起释放,此时剩下只有Dog对象,原本唯一指向Dog对象的_dog也不存在,Dog对象也被释放

    参考代码:
    Person.h
    1. //
    2. //  Person.h
    3. //  ARC
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @class Dog;
    10. @interface Person : NSObject
    11. // Person对象中的成员变量_dog为强引用
    12. @property (nonatomic, strong) Dog * dog;
    13. @end
    复制代码
    Person.m
    1. //
    2. //  Person.m
    3. //  ARC
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import "Person.h"
    9. #import "Dog.h"
    10. @implementation Person
    11. - (void)dealloc
    12. {
    13.     // 标记,如果对象被释放即会输出这句
    14.     NSLog(@"%@ is dealloc", self);
    15. }
    16. @end
    复制代码
    Dog.h
    1. //
    2. //  Dog.h
    3. //  ARC
    4. //
    5. //  Created by yusian on 14-3-20.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @class Person;
    10. @interface Dog : NSObject
    11. // Dog对象中的成员变量_person为弱引用
    12. @property (nonatomic, weak) Person * person;
    13. @end
    复制代码
    Dog.m
    1. //
    2. //  Dog.m
    3. //  ARC
    4. //
    5. //  Created by yusian on 14-3-20.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import "Dog.h"
    9. #import "Person.h"
    10. @implementation Dog
    11. - (void)dealloc
    12. {
    13.     // 标记,如果对象被释放即会输出这句
    14.     NSLog(@"%@ is dealloc", self);
    15. }
    16. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  ARC
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "Person.h"
    10. #import "Dog.h"
    11. int main()
    12. {
    13.     NSLog(@"Start main...");
    14.    
    15.     // 创建Person对象p
    16.     Person * p = [[Person alloc] init];
    17.    
    18.     // 创建Dog对象d
    19.     Dog * d = [[Dog alloc] init];
    20.    
    21.     // 将d赋值给p对象中的成员对象_dog;
    22.     p.dog = d;
    23.    
    24.     // 将p赋值给d对象中的成员变量_person;
    25.     d.person = p;
    26.    
    27.     NSLog(@"End main...");
    28.     return 0;
    29. }
    复制代码
    运行结果:
    2014-03-20 11:31:42.645 ARC[1088:303] Start main...
    2014-03-20 11:31:42.647 ARC[1088:303] End main...
    2014-03-20 11:31:42.647 ARC[1088:303] <Person: 0x100301d70> is dealloc
    2014-03-20 11:31:42.647 ARC[1088:303] <Dog: 0x1003062d0> is dealloc
    Program ended with exit code: 0

    内存图示ppt下载: ARC.pptx.zip (53.24 KB, 下载次数: 0)
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-5 21:38 , Processed in 0.059839 second(s), 26 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表