发现一个有趣的现象,OC 重写get方法,然后调用get方法会导致属性的引用记述不断增加,self.xxx如果和get方法交叉调用,self.xxx输出的引用计数也会不断增加,否则只会比_xxx的引用计数多一。属性若是原子的,那么不重写get方法,计数也会增加
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// av_register_all();
UIImageView *i = [[UIImageView alloc] init];
_image = i;
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)_image));
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)_image));
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)([self image])));
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.image)));
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.image)));
NSLog(@"change");
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)([self image])));
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)([self image])));
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.image)));
printf("%ld\n",CFGetRetainCount((__bridge CFTypeRef)(((UIImageView* (*) (id, SEL)) objc_msgSend) (self, sel_registerName("image")))));
}
- (UIImageView *)image{
NSLog(@"%s",__func__);
return _image;
}
网友评论