-2

Possible Duplicate:
Difference between [NSMutableArray array] vs [[NSMutableArray alloc] init]

NSMutableArray* test = [NSMutableArray array];
NSMutableArray* test1 =[[NSMutableArray alloc]init];

What is the difference?

Community
  • 1
  • 1
kumar123
  • 791
  • 7
  • 21

4 Answers4

3

You don't need to release test (as you didn't allocate it), but you do need to release test1 (as you allocated it) (assuming no ARC involved).

The method [NSMutableArray array] already returns an autoreleaseed array.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

test is autoreleased. That means that it has been added to the current autorelease pool, and when the pool is drained (typically, when the current run loop ends) it will be sent a release message. If no one else has sent it retain, the memory can be freed at that point.

test1 is not, and has an effective retain count of +1. Your responsibility to release, or you'll leak the memory.

ckhan
  • 4,771
  • 24
  • 26
0

The first one is a static method of NSMUtableArray class, which return an initialized and autorelized array. In the second statement you allocate and initialize it by hand

jbduzan
  • 1,106
  • 1
  • 14
  • 30
0

The Relation Between the two statements :

[NSMutableArray array] equivalent to  [[[NSMutableArray alloc] init] autorelease];
Bobj-C
  • 5,276
  • 9
  • 47
  • 83