Re: should release or not? (basic memory
management question)

On Sun, Mar 29, 2009 at 12:24 PM, Ignacio Enriquez <nacho4d@xxxxxxxxx> wrote:
> Hi everyone.
> I still have some doubts of Âshould I release or not? and the most
> important reaons why should I or why shouldn't I do it. Any answer
> would be very appreciated.
> Consider this:
> NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
> NSArray *rawArray = [defaults objectForKey:@"lastArray"];
> NSMutableArray *tempArray = [[NSMutableArray alloc] init];
> for(int i= 0; i< [rawArray count]; i++){
> Â NSDictionary *dic = [rawArray objectAtIndex:i];
> Â Mail *mail = [[Mail alloc] initWithDictionary:dic];
> Â [tempArray addObject:mail];
> Â [mail release];
> }
> theArray = tempArray;
> Here theArray is a property:
> @property (nonatomic, retain) NSArray *theArray;

>From Cocoa's memory management rules

- You own any object you create.
You "create" an object using a method whose name begins with âallocâ
or ânewâ or contains âcopyâ (for example, alloc, newObject, or

- If you own an object, you are responsible for relinquishing
ownership when you have finished with it.
One way to relinquish ownership of an object is to send it a release
message. In Cocoa terminology, relinquishing ownership of an object is
typically referred to as "releasing" an object.

- If you do not own an object, you must not release it.

> So my doubts are:
> Since is initialized by a class method , "defaults" must not be
> released -> correct?

Correct. You didn't create it or retain it, you don't release it

> rawArray should be released? ([rawArray release] ?) I think the answer
> is no but why?

Correct. You didn't create it or retain it, you don't release it

> dic should be released? ([dic release]?) I don't know...

You didn't create it or retain it, you don't release it

> tempArray should be released? I think the answer is: it depends.
> Since theArray is retain I could do
> self.theArray = tempArray;
> [tempArray release];
> or
> theArray = tempArray;
> so, what is the difference between using self and assigning tempArray
> to theArray directly?

Assigning directly to the instance variable is just that; an
assignment. No retain happens, no release happens, no method is
called. Assigning through the property (i.e. self.theArray =) calls a
method. That method will release the old value of theArray and retain
the new value.

> (specifically in this case what is the
> difference (in terms of retain count)?)
> And what would happen with the retaincount of another object that was
> theArray before tempArray assignation? (in case of doing theArray =
> tempArray)

In this case, not using the property will cause whatever theArray
*used* to point to to be leaked.

Clark S. Cox III

Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:

This email sent to maillists@xxxxxxxxx