views:

5587

answers:

4
NSMutableArray *a1 = [[NSMutableArray alloc] init];
NSMutableArray *a2 = [NSMutableArray array];

TempObj *obj = [[TempObj alloc] init]; //assume this line is repeated for each obj
[a1 addObject:obj];
[a1 addObject:obj2];
[a1 addObject:obj3];
[a1 addObject:obj4];

[obj release];
[obj2 release];
[obj3 release];
[obj4 release];

[a1 release];

Ok so a2 is an autorelease obj so i dont have to call release on it? Also how do you know when you get an autorelease object?

And for a1, i dont have to loop through the array and release each object first? What if i called [a1 removeAllObjects]; does that call [[a1 objectAtIndex:#] release];

Am i supposed to release those objects after ive added them to the array?

+11  A: 

When you add an object to an array, it calls retain on that object. If you don't release your pointer to that object, it will be a leak. When you release the array, it will call release on all of the objects that it holds, since it called retain previously.

As for autorelease vs release, the only way to know for sure (aside from possibly reading the documentation) is by the name of the method. I believe the rule in general is that if you didn't allocate the object, then you aren't responsible for releasing it.

Regarding the object creation methods, all of the convenience methods (array:, arrayWithObjects:, arrayWithArray:, etc.) return autoreleased objects. However, their corresponding init methods (init:, initWithObjects:, initWithArray:, etc.) do not - if you call them, you are responsible for calling release on the returned object.

I seem to recall a few other questions on this topic here - you might try searching around for a more thorough explanation.

Andy
so doing a loop through all the objects and releasing 1 by 1 and than calling release on the array itself is wrong.
chicken
That is correct - the array handles all that for you.
Andy
+8  A: 

It's easier to think of Objective-C memory management as ownership rather than in terms of retain-release. When you add the objects to the array, the array is now a co-owner of the object and is responsible for properly managing the memory. When the owner (whatever object contains the code you posted) calls release on the objects, it's giving up ownership and now the array is the sole owner.

Apple has a good primer on how ownership works in Cocoa (including how you know when you are responsible for calling release on an object): Memory Management Programming Guide For Cocoa. It's a must-read if you want to use Cocoa.

Chuck
+1  A: 

The basic thing to remember is this: You must balance every call to "init", "retain" or "copy" with a corresponding call to "release" or "autorelease". That's really all that you need to know.

In your example, a1 had a call to "init", so you need to have a "release" somewhere on it. Ditto with "obj". You didn't call "init", "retain", or "copy" on anything else, so you don't need to call "release" or "autorelease" on anything else.

smeger
But when do i release Obj, because i add it to the mutable array do i release the obj i create right after adding it, because the array creates a copy, or do i release it when i release the array?
chicken
You release it as soon as you no longer need to do anything with it. Once you've added it to the array, you no longer need to do anything else with it, so you can release it.The array (internally) needs to keep it around so it retains it, but this is an internal implementation detail.
smeger
A: 

i dont know either im right or wrong NSMutableArray *a1 = [[NSMutableArray alloc] init]; will be autorelease NSMutableArray *a1 = [NSMutableArray alloc] will not autorelease

after you [a1 removeAllObjects];

Ahmad Ashraf bin Azman