本帖最后由 D调的沉默 于 2014-5-3 16:28 编辑
代码如下- int main(int argc, const char * argv[])
- {
- Person * p=[Person new];
- Car * car=[Car new];
- car.wheels = 2;
-
- p.car = car;
-
- Car * car2 = [Car new];
- car2.wheels = 4;
-
- p.car = car2;
-
- NSUInteger a = [car retainCount];
-
- NSLog(@"a=%ld",a);
-
- [car release];
-
- a = [car retainCount];
- NSUInteger b = [car2 retainCount];
- NSLog(@"a=%ld,b=%ld",a,b);
- [p release];
- b = [car2 retainCount];
- NSLog(@"b=%ld",b);
- [car2 release];
-
- return 0;
- }
复制代码 主函数中代码如上,在【p release】之后关于car2 对象的使用都回出现错误,但是在【p release】这之前car2的引用次数为2啊,【p release】了它的引用次数应该为1啊,为什么就直接不能使用了。。。在
person类中得-delloc方法代码如下
- - (void)setCar:(Car *)car
- {
- _car = car;
- _car = [_car retain];
- }
- - (void)dealloc
- {
- [_car dealloc];
- [super dealloc];
- NSLog(@"person delloc");
- }
复制代码
求解释啊。。
|