Possible Duplicate:
Use autorelease when setting a retain property using dot syntax?
What is difference between using ivars and self. notation?
instanceVar is instance variable declared with retain.
1) instanceVar = [[NSMutableArray alloc] initWithObjects:@”1″, @”2″]; //do I need autorelease here?????
2) self.instanceVar = [[NSMutableArray alloc] initWithObjects:@”1″, @”2″] autorelease];
Also, Do I need autorelease in the first situation?
This is explained in multiple places but seems as you asked what the different is
The first call is unchanged and looks like this:
The second call when compiled will look something like this (assuming you have used a
@propertywithretainand@synthesize:The body of the
- (void)setInstanceVar:(NSMutableArray *)instanceVar;method will look something like this (the compiler create this for you because of your@propertyand@sythesize):Therefore in the call
You have the +1 retain count on the newly created
NSMutableArrayand then you have the +1 retain count added from going through the setter.This means that you require the extra release to match retains you are taking. It is considered better to not use
autoreleasein iPhone so you can be sure memory is being freed when you want it to. Therefore you should normally take the patternWhich looks like this (FIXED thanks to @jamapag)