遗留问题
#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
问题:
当t1 = t0后,无论是否用weak 修饰,t1 的引用计数变成了2?
儒君拜读过bbum 的文章,大概意思是引用计数并不能保证绝对的正确。
retainCount is useless.
Understanding Memory Management retain count