Faqts : Computers : Programming : Application Frameworks : Macintosh : Cocoa

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

When is it my responsibility to release an object?

Apr 30th, 2002 16:41
Ben Hines, Christopher Holland,


Ahh, memory management in Cocoa. 
The basic principle is that you are responsible for releasing an object 
if it is an object that YOU allocated yourself (using alloc or an 
init... method).
If you received your object from another person's code, you must assume 
that the object release is handled by that code.
For instance, if you use 
NSArray *myArray = [[NSArray alloc] initWithArray:someArray];
then you are responsible for releasing myArray when you are done with
it. Otherwise, you will be leaking.
On the other hand, if you use
NSArray *myArray = [NSArray arrayWithArray:someArray];
then myArray will be returned to you autoreleased so that when the code
leaves the current context it will already be on the autorelease stack
and will be released.