我有以下簡單的代碼:爲什麼retainCount在釋放被調用後返回值?
NSMutableArray *array = [[NSMutableArray alloc] init];
NSObject *o = [[NSObject alloc] init];
NSObject *o1 = [[NSObject alloc] init];
NSObject *o2 = [[NSObject alloc] init];
[array addObject:o];
[array addObject:o1];
[array addObject:o2];
NSLog(@"-------");
NSLog(@"%d, %d, %d, %d\n", [o retainCount], [o1 retainCount], [o2
retainCount], [array retainCount]);
[array release];
NSLog(@"%d, %d, %d, %d\n", [o retainCount], [o1 retainCount], [o2
retainCount], [array retainCount]);
[o release];
[o1 release];
[o2 release];
NSLog(@"-------");
NSLog(@"%d, %d, %d, %d\n", [o retainCount], [o1 retainCount], [o2
retainCount], [array retainCount]);
作爲輸出我得到:
[Session started at 2010-10-27 18:00:59 +0200.]
2010-10-27 18:01:02.186 Questions[22463:207] -------
2010-10-27 18:01:02.187 Questions[22463:207] 2, 2, 2, 1
2010-10-27 18:01:02.188 Questions[22463:207] 1, 1, 1, 1
2010-10-27 18:01:02.188 Questions[22463:207] -------
,並且程序用EXC_BAD_ACCESS壓碎。
我的問題是以下幾點: 我明白,打完電話後[陣列發行]數組對象不存在了,對不對?對於其他對象的調用釋放也是如此,對嗎?如果是這樣,爲什麼我調用[array retainCount]後沒有得到EXC_BAD_ACCESS?爲什麼它返回任何值?以及爲什麼在其他對象上調用retainCount會導致EXC_BAD_ACCESS?
感謝您的幫助!