1 //
2 // main.m
3 // autorelease.m
4 //
5 // Created by sixleaves on 15/5/9.
6 // Copyright (c) 2015年 itcast. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 #import "Dog.h"
12 int main(int argc, const char * argv[]) {
13 @autoreleasepool { // pool1入棧
14
15 // p-1
16 Person *p = [[[Person alloc] init] autorelease];
17 // c-1
18 Dog *c = [[[Dog alloc] init] autorelease];
19
20 // p -1 c-2
21 p.dog = c;
22
23
24 @autoreleasepool { // pool2入棧
25
26 // p1-1
27 Person *p1 = [[[Person alloc] init] autorelease];
28 // c1-1
29 Dog *c1 = [[[Dog alloc] init] autorelease];
30
31 // p1 -1 c-2
32 p1.dog = c1;
33
34
35 } // pool2出棧銷毀
36
37
38
39 } // pool1出棧銷毀。
40
41 return 0;
42 }
43 /*
44 自動釋放池的管理是通過棧這個數據結構進行管理。
45
46 autorelease的優點:
47 1.不要在關心對象的釋放時間。
48 2.不用再關心什么時候用release。
49
50 autorelease的使用場合(注意點):
51 1.對于占用內存交大的對象,不要使用autorelease,因為不能精確的控制對象釋放的時間。
52 2.反之,對于占用內存小的,使用autorelease的影響比較小。
53
54 autorelease添加到那個池子:
55 1.通過棧的結構,我們可以很容易的理解,autorelease是添加到當前管理池子的棧
56 中棧頂得池子。
57
58
59 autorelease的常見錯誤(本質都是最終會多次調用release):
60 1.連續調用autorelease。
61 2.在自動釋放池中有效范圍中,同時條用了autorelease與release。
62
63
64 // 在啰嗦幾句:
65 // 在ios運行中,系統會自動創建很多的池子,所以autorelease作用在
66 // 的池子,是指棧頂的池子。
67 //
68 //
69
70 */
71