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!
Well, as you correctly guessed you can’t send messages to objects that have already been released. The behavior if you do so is unspecified, so
EXC_BAD_ACCESSmight be raised, but it need not be.If you want to get
retainCountfrom objets you already released, you should check outNSZombieEnabled(http://www.cocoadev.com/index.pl?NSZombieEnabled), which will cause objects to be released but not dealloc’ed.