在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
View Code
1 @interface ClassC : NSObject { 调用如下: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; ClassB * b = [[ClassB alloc] init]; [b release]; [pool release]; }
根据调试的结果,在ClassB的dealloc中使用:self.classc = Nil;也可以执行到ClassC的dealloc方法, 说明使用self.classc = Nil & self.classc = nil & [class_c release]的效果都是一样的。 如果在ClassB的init方法不去初始化 class_c 执行[class_c release] 也不会出错,因为ios中向一个空的对象发送消息是没有问题的。
在ClaasB中 -(void) dealloc { [super dealloc]; // self.classc = nil; [class_c release]; // class_c = nil; [class_c release]; class_c = nil; } 也不会有问题。
但是,在ClassB中如下就出问题了 @interface ClassB : NSObject { @private ClassC * class_c; ClassC * class_c1; ClassC * class_c2; } @property(nonatomic, retain) ClassC * classc; @property(nonatomic, retain) ClassC * classc1; @property(nonatomic, retain) ClassC * classc2; -(void) test; @end @implementation ClassB @synthesize classc = class_c; @synthesize classc1 = class_c1; @synthesize classc2 = class_c2; -(id) init { if (self = [super init]) { class_c = [[ClassC alloc] init]; class_c1 = [[ClassC alloc] init]; class_c2 = [[ClassC alloc] init]; } return self; } -(void) test { self.classc = self.classc1; self.classc1 = self.classc2; self.classc2 = nil; } -(void) dealloc { [super dealloc]; self.classc = Nil; // = nil 也是一样的 self.classc1 = Nil; self.classc2 = Nil; // [class_c release]; // class_c = nil; // // [class_c1 release]; // class_c1 = nil; // // [class_c2 release]; // class_c2 = nil; } @end 这样在[b release];的时候 就报了 EXC_BAD_ACCESS错误, 好换种写法: -(void) dealloc { [super dealloc]; // self.classc = nil; // self.classc1 = nil; // self.classc2 = nil; [class_c release]; class_c = nil; [class_c1 release]; class_c1 = nil; [class_c2 release]; class_c2 = nil; } 这次就正确了,没有任何错误,得到结论1:在释放对象的时候,使用 [xxx release]; 尽量不要使用self.xxxx = nil这种方式。 此时多调用 几次 [b test]; 也没有错误, 再试下不初始化 三个class_c 也是没有问题的。
那这两个结论不是矛盾的么? 其中的原因到底是为什么?我接着做了如下尝试: @interface ClassB : NSObject { @private ClassC * class_c; ClassC * class_c1; ClassC * class_c2; } @property(nonatomic, retain) ClassC * classc; @property(nonatomic, retain) ClassC * classc1; @property(nonatomic, retain) ClassC * classc2; -(void) test; @end @implementation ClassB @synthesize classc = class_c; @synthesize classc1 = class_c1; @synthesize classc2 = class_c2; -(id) init { if (self = [super init]) { class_c = [[ClassC alloc] init]; class_c1 = [[ClassC alloc] init]; class_c2 = [[ClassC alloc] init]; } return self; } -(void) test { NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc = self.classc1; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc1 = self.classc2; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc2 = nil; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); } -(void) dealloc { [super dealloc]; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc = nil; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc1 = nil; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc2 = nil; // [class_c release]; // class_c = nil; // // [class_c1 release]; // class_c1 = nil; // // [class_c2 release]; // class_c2 = nil; } 这样结果也是一样的,很崩溃,在ClassB的dealloc方法里面,没关系再试试,我重新尝试了一下 -(void) dealloc { NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc = nil; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc1 = nil; NSLog(@"class_c is :%d", class_c.retainCount); NSLog(@"class_c1 is :%d", class_c1.retainCount); NSLog(@"class_c2 is :%d", class_c2.retainCount); self.classc2 = nil; [super dealloc]; } 这时候竟然神一样的没有问题了, 不同的地方就是[ super dealloc ] 这句,放到后面就正确了,再做如下两个实验: -(void) dealloc { [class_c release]; class_c = nil; [class_c1 release]; class_c1 = nil; [class_c2 release]; class_c2 = nil; [super dealloc]; }
-(void) dealloc { [super dealloc]; [class_c release]; class_c = nil; [class_c1 release]; class_c1 = nil; [class_c2 release]; class_c2 = nil; } 这两种都是可以正确执行的,没有问题,那么问题的核心就在 [super delloc] 到底对 sekf.xxxx = nil 有什么影响?
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论