Memory Management in Objective-C

Rules:

You only release or autorelease your own objects

1 Your own objects including these you created it with a method begins with "alloc" or "new" or contains "copy"(alloc,newObject,or mutableCopy),or if you send it a retain message.

2 You use release or autorelease to relinquish ownership of an object. autorelease just means "send a release message in the future".

Object Ownership Policy

You own any object you create.

You "create" an object using a method whose name begins with "alloc" or "new" or contains "copy".

You can take ownership of an object using retain

Remember that an object may have more than one owner. Taking ownership of an object is your own way of saying that you need it to be kept alive.

You must relinquish ownership of objects you own when you are finished with them.

You relinquish ownership of an object by sending it a message release or autorelease. In Cocoa terminology, relinquish ownership of an object is therefore typically referred to as "releasing" an object.

You must not relinquish ownership of an object you do not own.

This is primarily an implicit corollary of the previous policy rules, made explicit.

Retain Count

When you create an object, it has a retain count of 1 .

When you send an object a retain message, its retain count is incremented by 1.

When you send an object a release message, its retain count is decremented by 1.

When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future.

if an object's retain count is reduced to 0, it is deallocated.

Autorelease

When you using method like this:

NSArray *array = [[NSSArray alloc] initWithObjects:aObject,otherObject,nil];

you should release array when you don't need it

But when you using method:

NSArray *array = [NSArray arrayWithObjects:aObject,otherObject,nil];

you need not release it ,because you don't own it.

Accessor Methods

- (void) setTitle:(NSString *) newTitle

{

if (title != newTitle)

{

[title release];

title = [newTitle retain];

}

}

Deallocating an Object

If your class has object instance variables that it owns, you must implement a dealloc method that release them.When an object is deallocated, its dealloc method is invoked automatically.

- (void) dealloc {

[title release];

[m_name release];

[super release];

}

# An object may have one or more owners.

By way of an analogy, consider a timeshare apartment.

# When an object has no owners, it is destroyed

To stretch an analogy, consider a timeshare complex that is not loved by the local population. If there are no owners,the complex will be torn down.

# To make sure an object you are interested in is not destroyed, you must become an owner.

You can either build a new apartment, or take a stake in an existing one.

# To allow an object in which you're no longer interested to be destroyed , you relinquish ownership,You can sell your timeshare apartment.

alloc

Allocates memory for an object, and return it the retain count of 1.

You own objects you create using any method that starts with the word alloc or with the word new.

copy

Makes a copy of an object, and returns it with retain of 1.

If you copy an object, you own the copy. This applies to any method that contains the word copy where "copy" refers to the object being returned.

retain

Increase the retain count of an object by 1.

Takes ownership of an object.

release

Decrease the retain count of an object by 1.

Relinquishes ownership of an object.

autorelease

Decreases the reference count of an object by 1 at some stage in the future.

Relinquishes ownership of an object an some stage in the future.

NSMutableArray *array;

NSInteger i;

for (i = 0; i < 10; i++) {

NSNumber *convenienceNumber = [NSNumber numberWithInteger: i];

[array addObject:convenienceNumber];

}

NSMutableArray *array;

NSInteger i;

for (i = 0; i < 10; i++ ) {

NSNumber *allocedNumber = [[NSNumber alloc] initWithInteger: i];

[array addObject:allocedNumber];

[allocedNumber release];

}

Autorelease Pool

An autorelease pool is an instance of NSAutoreleasePool that contains other objects that have received an autorelease message; when the autorelease pool is deallocated it sends a release message to each of those objects.

NSAutoreleasePool *myPool = [[NSAutorelease alloc] init];

.......

[myPool drain];

Accessor Methods

Three ways to implement the accessors:

Getter retain and autoreleases the value before returning it;setter releases the old value and retains(or copies) the new value.

- (NSString *) title {

return [[title retain] autorelease];

}

- (void) setTitle: (NSString *) newTitle {

if(title != newTitle) {

[title release];

title = [newTitle retain];

}

}

Getter returns the value; setter autoreleases the old value and retains(or copies) the new value.

- (NSString *) title {

return title;

}

- (void) setTitle: (NSString *) newTitle {

[title autorelease];

title = [newTitle retain];

}

Getter returns the value; setter releases the old value and retains(or copies) the new value.

- (NSString *) title {

return title;

}

- (void) setTitle:(NSString *) newTitle {

if(newTitle != title) {

[title release];

title = [newTitle retain];

}

}