Difference between [NSMutableArray array] vs [[NSMutableArray alloc] init]

Adelino picture Adelino · Mar 24, 2011 · Viewed 29.3k times · Source

can someone tell me the difference in declare an mutable array with:

NSMutableArray *array = [NSMutableArray array];

and

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

Because in the beginning I was declaring all my arrays with alloc, and if in the end of a certain function I returned the array created with alloc, I had to autorelease that array, because of memory leak problems.

Now using the first declaration I don't need to release anything.

Thanks

Answer

BoltClock picture BoltClock · Mar 24, 2011

The array class method by itself produces an autoreleased array, meaning you don't have to (and should not) release it manually.