views:

40

answers:

1

Not quite sure how to phrase this, but should I release a variable in this situtation:

NSString *string = @"HELLO WORLD";
NSArray *array = [NSArray arrayWithObject:string];  
NSString *shouldIReleaseThis = [array objectAtIndex:0];
NSLog(@"%@", shouldIReleaseThis);
//????  [shouldIReleaseThis release] ??????

//Do stuff with array

Should I release it? Why or why not?

+3  A: 

You don't own it (you didn't get that reference from new, alloc, retain or copy), so you shouldn't release it. See Apple's memory management programming guide for a brief but complete overview of the memory management rules in Cocoa.

Chuck
Thanks, that's just what I was looking for. I'd read about memory management, but somehow the concept of "ownership" hadn't really sunk in.
rob
@Chuck: This is a really good answer that applies to all variable releasing in Cocoa. +1!
Chetan