I'm a objective c newbie, and i'm having a bit of problems with memory management, I've read the apple's memory management policies, however i need a bit of clarification here, this is pretty simple i guess, but i would like to ask you if I'm right:
Given this property:
@interface Test : NSObject {
NSArray *property1;
}
@property (nonatomic,retain) NSArray* property1;
@end
...
//And its implementation:
@implementation Test
@synthetize property1;
-(id) init {
if (self=[super init]) {
self.property1=[[[NSArray alloc] initWithCapacity:5] autorelease];
}
return self;
}
-(void) dealloc {
[super dealloc];
[property1 release];
}
@end
Is it right to issue an Autorelease message to the allocated object in the init method?, i do this cause in apple's document, says that every allocated object should be released by the developer, then, I think, alloc sets retain count to 1, then the property (nonatomic, retain) adds 1, so retain==2, then autorelease substracts 1, and when the dealloc method is called, property1 is released and retain count==0, am I right?
[super dealloc]
has to be be the last thing in your dealloc method. Doing anything after you call[super dealloc]
is a bug. – Chuck[NSArray arrayWithCapacity:5]
. That is identical to what you have above. – Dave DeLong[NSMutableArray arrayWithCapacity:]
, but you'll want to make the property type match if the array should be mutable. – Quinn Taylor