Hello,
I have the following simple piece of code:
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]);
as an output I'm getting:
[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] -------
and the program crushes with EXC_BAD_ACCESS.
my question is following: I understand that after calling [array release] the array object does not exist anymore, right? and the same is for calling release for the other objects, yes? If so, why I don't get EXC_BAD_ACCESS after calling [array retainCount]? why it returns any value? and why calling retainCount on the other objects causes EXC_BAD_ACCESS?
Thanks for your help!