59

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

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Adelino
  • 2,530
  • 4
  • 20
  • 32

2 Answers2

38

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
33

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 memory leak problems. Now using the first declaration i don't need to release anything

That is exactly correct when you "vend" an object. But in other cases, when you create an object on iOS, where you have a choice between obtaining a ready-made autoreleased object and calling alloc followed by release, Apple wants you to use alloc and release, because this keeps the lifetime of the object short and under your control.

The problem here is that autoreleased objects live in the autorelease pool and can pile up until the pool is drained, whenever that may be.

Another thing to watch out for is loops. You may generate autoreleased objects without being aware of it, and they just pile up in the pool. The solution is to create your own autorelease pool at the start of the loop and release it at the end of the loop, so that the objects are released each time thru the loop.

EDIT - 12/18/2011: But with iOS 5 and the coming of ARC, the autorelease mechanism is far more efficient, and there is no such thing as release, so the distinction between alloc-init and a convenience constructor vending an autoreleased object becomes moot. (Also it's now an @autoreleasepool block rather than an autorelease pool (pseudo-)object.)

iPatel
  • 46,010
  • 16
  • 115
  • 137
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I should add that I've just written a book on iOS programming, and if you're just getting into this, the memory management chapter might be useful to you. It explains the details of what you're asking about here: http://www.apeth.com/iOSBook/ch12.html – matt Mar 24 '11 at 17:51
  • 1
    The term "vend" is generally used in Cocoa to mean "make available through PDO." What do you mean here? – Chuck Mar 24 '11 at 18:34
  • I believe by "vend" here matt means return an object to be used elsewhere--he was saying that it's correct to *return* an object autoreleased, as opposed to when one is using an object within their own method only, in which case he was recommending alloc/init. – andyvn22 Apr 19 '12 at 10:14
  • 1
    FYI, the `PDO` mentioned in another comment is `Portable Distributed Objects` for calling objects over a network. https://en.wikipedia.org/wiki/Portable_Distributed_Objects – Basil Bourque Sep 11 '13 at 22:38