Hi to all!
I dropped 2 days on this problem and can't come out with a solution so: (I know this wont have much sense but hopefully it will do the trick)
monkey.h
@interface monkey : NSObject {
NSNumber *monkeyRanch;
}
@property (nonatomic, retain) NSNumber *monkeyRanch;
-(id) gatherTheMonkeys:(int)howmany;
monkey.m
@synthesize monkeyRanch;
-(id) gatherTheMonkeys:(int)howmany{
NSNumber *temp=[[NSNumber alloc] initWithInt:howmany];
monkeyRanch = temp;
[temp release];
return [monkeyRanch autorelease];
}
appDelegate.m
theMonkeys = [[monkey alloc] gatherTheMonkeys:3];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
..and theMonkeys are released in dealloc. This works as expected. No leaks in Instrument. BUT. If I try the same with an NSMutable array instead of the NSNumber:
monkey.h
@interface monkey : NSObject {
NSMutableArray *monkeyRanch;
}
@property (nonatomic, retain) NSMutableArray *monkeyRanch;
-(id) initTheMonkeys:(int)howmany;
monkey.m @synthesize monkeyRanch;
-(id) initTheMonkeys:(int)howmany{
monkeyRanch=[[NSMutableArray alloc] init];
NSNumber *imp =[[NSNumber alloc] initWithInteger:howmany];
[monkeyRanch addObject:imp];
[imp release];
return [monkeyRanch autorelease];
}
appDelegate
theMonkeys = [[monkey alloc] initTheMonkeys:3];
[theMonkeys retain];
this causes a leak (of an object 'monkey') just at the start of the application.
Also tried to change the initTheMonkeys to the following:
NSMutableArray *temp=[[NSMutableArray alloc] init];
monkeyRanch=temp;
[temp release];
NSNumber *imp =[[NSNumber alloc] initWithInteger:howmany];
[monkeyRanch addObject:imp];
[imp release];
return [monkeyRanch autorelease];
but the retain count of monkeyRanch got to zero as releasing temp and the app crashed happily.
I know there is something simple and obvious I'm missing.. (and yes I read the mem management docs but still missing something)
Any help much appreciated.