【发布时间】:2014-03-16 17:55:48
【问题描述】:
我有以下代码:
- (IBAction)HeyCount:(UIButton *)sender {
NSString* strr = [[NSString alloc] initWithString:@"hi there"];
self.string = @"789";
ohYeah = @"456";
NSLog(@"Retain Count of ohYeah:[%d] with String:[%ld]",[ohYeah retainCount],(long)[ohYeah integerValue]);
NSLog(@"Retain Count of strr:[%d] with String:[%ld]",[strr retainCount],(long)[strr integerValue]);
}
上面代码的输出是:
Retain Count of ohYeah:[-1] with String:[456]
Retain Count of strr:[-1] with String:[0]
ohYeah 的声明在 .h 文件中
NSString * ohYeah;
我没有使用ARC。你们中的任何人都可以解释为什么strings 中的retain count 是-1 并且使用retain count -1 访问对象不应该崩溃吗?
【问题讨论】:
-
不要使用保留计数
-
我使用保留计数只是为了学习内存管理。
-
@OMerObaid 请参考前面三个关于何时使用保留计数来处理有关内存管理的内容。另外,你为什么不使用ARC? (提示 - 使用 ARC)。
-
@OMerObaid retainCount 对于学习内存管理不是很有用,在 MRR 和 ARC 下都没有。例如,编译器会根据优化级别和/或版本发出一组不同的保留和释放。
标签: objective-c ios7 retaincount