Hi Stephen,
Just some practice with retain counts. Just before [pool drain] I sent the release message to first
[first release] which brought the retain count to 0. In the output below does the error mean that the pointer is no longer allocated and so it can not be released. Taking the last [first release] statement corrects this error. If so why? It's a little mindbending because it seems that problems could arise when releasing an object to early. Is this correct?
#import <Foundation/Foundation.h>
@interface RetainQuestion : NSObject
{
int someNumber;
}
@property int someNumber;
@end
@implementation RetainQuestion
@synthesize someNumber;
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
RetainQuestion *first = [[RetainQuestion alloc] init];
NSMutableArray *myArr = [NSMutableArray array];
NSLog(@"first retain count = %lx",[first retainCount]);
[myArr addObject: first];
NSLog(@"first retain count = %lx",[first retainCount]);
[first release];
NSLog(@"first retain count = %lx",[first retainCount]);
[first release];
NSLog(@"first retain count = %lx",[first retainCount]);
[first release]; // ANALYZE ERROR: Incorrect decrement of the reference count of an object that is not
// is not owned at this point by the caller
[pool drain];
return 0;
}
2011-09-26 15:53:09.277 RetainQuestion[1067:707] first retain count = 1
2011-09-26 15:53:09.279 RetainQuestion[1067:707] first retain count = 2
2011-09-26 15:53:09.279 RetainQuestion[1067:707] first retain count = 1
2011-09-26 15:53:09.280 RetainQuestion[1067:707] first retain count = 1
RetainQuestion(1067,0x7fff755dc960) malloc: *** error for object 0x100113e60: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Any help is appreciated
Cheers
David