0

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?

Community
  • 1
  • 1
Henley
  • 21,258
  • 32
  • 119
  • 207
  • Among plenty of others: [Search: objc property autorelease](http://stackoverflow.com/search?q=[objc]+retain+property+autorelease) – jscs Jul 30 '11 at 22:20

2 Answers2

2

This is explained in multiple places but seems as you asked what the different is

The first call is unchanged and looks like this:

instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"];

The second call when compiled will look something like this (assuming you have used a @property with retain and @synthesize:

self.instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"];
// The previous line will compile to this next line
[self setInstanceVar:[[NSMutableArray alloc] initWithObjects:@"1", @"2"]];

The body of the - (void)setInstanceVar:(NSMutableArray *)instanceVar; method will look something like this (the compiler create this for you because of your @property and @sythesize):

- (void)setInstanceVar:(NSMutableArray *)anInstanceVar
{
    if (instanceVar != anInstanceVar) {
        [instanceVar release];
        instanceVar = [anInstanceVar retain];
    }
}

Therefore in the call

self.instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"];

You have the +1 retain count on the newly created NSMutableArray and 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 autorelease in iPhone so you can be sure memory is being freed when you want it to. Therefore you should normally take the pattern

  1. Create local var
  2. Assign local var to ivar through setter
  3. release local var

Which looks like this (FIXED thanks to @jamapag)

NSArray *tmpMyArray - [[NSArray alloc] initWithObject:@"Hello"];
self.myArray = tmpMyArray;
[tmpMyArray release]; tmpMyArray = nil;
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • In last example you created autoreleased NSArray (without alloc), so you don't need to release it. Right code is: `[[NSArray alloc] initWithObject:@"Hello"]` – jamapag Jul 30 '11 at 22:44
  • Good spot was trying to rush and dry coded in the browser without testing – Paul.s Jul 30 '11 at 22:50
  • why is the last example better? In all of them, it seems the array will be in memory until it gets deallocated in dealloc. – Henley Jul 30 '11 at 23:36
  • You could have a situation where the ivar is being set multiple times in which case you want he old object to be deallocated as soon as it is appropriate. – Paul.s Jul 30 '11 at 23:44
0
1) instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"]; //do I need autorelease here?????

The NSmutableArray is created with a retain count of 1, you need to release your instanceVar in your dealloc() method

2) self.instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"] autorelease];

Here you are using the setter, and since it is declared with retain it will increase its retain count by 1, the alloc init already increased the retain count by 1, so the total retain count is 2. However the autorelease msg will decrease this by 1 probaby in the next run loop. So again you only have to release this on your dealloc() method.

In the first situation you probably DO NOT want to autorelease, since this is an IVar you will probably want to use it again, and if you autorelease it the retain count will be 0 soon (most likely in the next run loop)

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118