关于IOS内存管理和ARC模式下的引用计数

发布于 2022-09-11 15:50:05 字数 4706 浏览 43 评论 0

问题1:引用计数是针对的对象,还是针对存放数据的内存?
若说是指向对象。但当用Strong修饰时。我把 t2 = t0;t0,t1,t3的引用计数都增加了1 变成了3,说明是针对内存的。
若说时针对内存。但当用weak修饰t1时,t0,t1,t2 都是指向同一个内存地址:0x1c401eef0 但从日志中可以看出他们的引用计数时不相同的。所以就有问题1

问题2:当我用weak修饰t1时,把t1 = t0。因为时weak修饰的t1 所以t0 的引用计数没有改变,仍然是1; 但是为什么t1的引用计数仍然+1 变成了2? (见下方日志)

#import "ViewController.h"


@interface RCTest ()
@property(nonatomic,copy)NSMutableString *s;
@end
@implementation RCTest

@end

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    RCTest *t0 = [[RCTest alloc] init];
    RCTest *t1;
    RCTest *t2;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
    t1 = t0;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
    t2 = t0;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
    t1 = nil;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
}
@end

日志:

2018-11-25 23:15:50.096611+0800 TestApp[2144:124995] 
2018-11-25 23:15:50.096789+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x0,t2:0x0
2018-11-25 23:15:50.096909+0800 TestApp[2144:124995] t0:1,t1:(null),t2:(null)
2018-11-25 23:15:50.096989+0800 TestApp[2144:124995] 
2018-11-25 23:15:50.097051+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x1c4014b10,t2:0x0
2018-11-25 23:15:50.097141+0800 TestApp[2144:124995] t0:2,t1:2,t2:(null)
2018-11-25 23:15:50.097216+0800 TestApp[2144:124995] 
2018-11-25 23:15:50.097278+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x1c4014b10,t2:0x1c4014b10
2018-11-25 23:15:50.098235+0800 TestApp[2144:124995] t0:3,t1:3,t2:3
2018-11-25 23:15:50.098356+0800 TestApp[2144:124995] 
2018-11-25 23:15:50.098425+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x0,t2:0x1c4014b10
2018-11-25 23:15:50.098522+0800 TestApp[2144:124995] t0:2,t1:(null),t2:2

当用weak修饰时:

#import "ViewController.h"


@interface RCTest ()
@property(nonatomic,copy)NSMutableString *s;
@end
@implementation RCTest

@end

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    RCTest *t0 = [[RCTest alloc] init];
    __weak RCTest *t1;
    RCTest *t2;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
    t1 = t0;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
    t2 = t0;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
    t1 = nil;
    NSLog(@"\n");
    NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
    NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
}
@end

日志

2018-11-25 23:17:09.154880+0800 TestApp[2161:126259] 
2018-11-25 23:17:09.155053+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x0,t2:0x0
2018-11-25 23:17:09.155442+0800 TestApp[2161:126259] t0:1,t1:(null),t2:(null)
2018-11-25 23:17:09.155544+0800 TestApp[2161:126259] 
2018-11-25 23:17:09.155610+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x1c401eef0,t2:0x0
2018-11-25 23:17:09.155736+0800 TestApp[2161:126259] t0:1,t1:2,t2:(null)
2018-11-25 23:17:09.155811+0800 TestApp[2161:126259] 
2018-11-25 23:17:09.155875+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x1c401eef0,t2:0x1c401eef0
2018-11-25 23:17:09.155969+0800 TestApp[2161:126259] t0:2,t1:3,t2:3
2018-11-25 23:17:09.156042+0800 TestApp[2161:126259] 
2018-11-25 23:17:09.156103+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x0,t2:0x1c401eef0
2018-11-25 23:17:09.156195+0800 TestApp[2161:126259] t0:2,t1:(null),t2:2


### 问题出现的环境背景及自己尝试过哪些方法


### 相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)


### 你期待的结果是什么?实际看到的错误信息又是什么?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文