iphone 内存管理 学习理解

来源:互联网 发布:女娇是什么网络用语 编辑:程序博客网 时间:2024/06/02 01:27

1.if you build an object using any method whose name includes alloc, new, create, or copy, you maintain responsibility for releasing the object

2. retain 属性

@property (retain) NSArray *colors;

// Non-autorelease object. Retain count is +1 at creation

NSArray *array = [[NSArray alloc]

initWithObjects:@"Black", @"Silver", @"Gray", nil];

// Count rises to +2 via assignment to a retained property

myCar.colors = array;

3.As a rule, whenever you ask another method to create an object, it’s good programming

practice to return that object autoreleased. Doing so consistently lets you follow a simple

rule:“If I didn’t allocate it, then it was built and returned to me as an autorelease object.”

- (Car *) fetchACar

{

Car *myCar = [[Car alloc] init];

return [myCar autorelease];

}

 

4.

You often see this pattern of create, assign, release in iPhone development.You might use

it when assigning a newly allocated view to a view controller object. For example:

UIView *mainView = [[UIView alloc] initWithFrame:aFrame];

self.view = mainView;

[mainView release];

These three steps move the object’s retain count from +1 to +2 and back to +1.

A final count of +1 guarantees you that can use an object indefinitely.At the same

time, you’re assured that the object deallocates properly when the property is set to a new

value and release is called on its prior value.That release brings the count down from +1

to 0, and the object automatically deallocates.

 

5.

- (void) dealloc

{

self.make = nil;

self.model = nil;

self.colors = nil;

[salesman release];

[super dealloc];

}

原创粉丝点击