OC MRC之autorelease问题(代码分析)

  1 //
  2 //  main.m
  3 //  08-autorelease
  4 //
  5 //  Created by apple on 13-8-9.
  6 //  Copyright (c) 2013年 itcast. All rights reserved.
  7 //
  8 
  9 /*
 10  1.autorelease的基本用法
 11  1> 会将对象放到一个自动释放池中
 12  2> 当自动释放池被销毁时,会对池子里面的所有对象做一次release操作
 13  3> 会返回对象本身
 14  4> 调用完autorelease方法后,对象的计数器不变
 15  
 16  2.autorelease的好处
 17  1> 不用再关心对象释放的时间
 18  2> 不用再关心什么时候调用release
 19  
 20  3.autorelease的使用注意
 21  1> 占用内存较大的对象不要随便使用autorelease
 22  2> 占用内存较小的对象使用autorelease,没有太大影响
 23  
 24  
 25  4.错误写法
 26  1> alloc之后调用了autorelease,又调用release
 27  @autoreleasepool
 28  {
 29     // 1
 30     Person *p = [[[Person alloc] init] autorelease];
 31  
 32     // 0
 33     [p release];
 34  }
 35  
 36  2> 连续调用多次autorelease
 37  @autoreleasepool
 38  {
 39     Person *p = [[[[Person alloc] init] autorelease] autorelease];
 40  }
 41  
 42  5.自动释放池
 43  1> 在iOS程序运行过程中,会创建无数个池子。这些池子都是以栈结构存在(先进后出)
 44  2> 当一个对象调用autorelease方法时,会将这个对象放到栈顶的释放池
 45  
 46  
 47  6.自动释放池的创建方式
 48  1> iOS 5.0前
 49  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 50  
 51  [pool release]; // [pool drain];
 52  
 53  
 54  2> iOS 5.0 开始
 55  @autoreleasepool
 56  {
 57     
 58  }
 59  */
 60 
 61 
 62 #import <Foundation/Foundation.h>
 63 
 64 @interface Person : NSObject
 65 
 66 @property (nonatomic, assign) int age;
 67 
 68 @end
 69 
 70 
 71 #import "Person.h"
 72 
 73 @implementation Person
 74 - (void)dealloc
 75 {
 76     NSLog(@"Person---dealloc");
 77     
 78     [super dealloc];
 79 }
 80 @end
 81 
 82 
 83 
 84 
 85 #import <Foundation/Foundation.h>
 86 #import "Person.h"
 87 
 88 int main()
 89 {
 90     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 91     
 92     Person *pp = [[[Person alloc] init] autorelease];
 93     
 94     [pool release]; // [pool drain];
 95     
 96     @autoreleasepool
 97     {
 98     
 99         // 1
100         Person *p = [[[[Person alloc] init] autorelease] autorelease];
101         
102         // 0
103         // [p release];
104     }
105     
106     return 0;
107 }
108 
109 
110 void test()
111 {
112     @autoreleasepool
113     {// { 开始代表创建了释放池
114         
115         // autorelease方法会返回对象本身
116         // 调用完autorelease方法后,对象的计数器不变
117         // autorelease会将对象放到一个自动释放池中
118         // 当自动释放池被销毁时,会对池子里面的所有对象做一次release操作
119         Person *p = [[[Person alloc] init] autorelease];
120         
121         p.age = 10;
122         
123         
124         
125         @autoreleasepool
126         {
127             // 1
128             Person *p2 = [[[Person alloc] init] autorelease];
129             p2.age = 10;
130             
131             
132         }
133         
134         
135         Person *p3 = [[[Person alloc] init] autorelease];
136         
137         
138     } // } 结束代表销毁释放池
139 }

第二部分

  1 //
  2 //  main.m
  3 //  09-autoreleased的应用
  4 //
  5 //  Created by apple on 13-8-9.
  6 //  Copyright (c) 2013年 itcast. All rights reserved.
  7 //
  8 #import <Foundation/Foundation.h>
  9 
 10 @interface Person : NSObject
 11 
 12 @property (nonatomic, assign) int age;
 13 
 14 + (id)person;
 15 
 16 + (id)personWithAge:(int)age;
 17 @end
 18 
 19 
 20 
 21 #import "Person.h"
 22 
 23 @implementation Person
 24 
 25 
 26 + (id)person
 27 {
 28     return [[[self alloc] init] autorelease];
 29 }
 30 
 31 
 32 + (id)personWithAge:(int)age
 33 {
 34     Person *p = [self person];
 35     p.age = age;
 36     return p;
 37 }
 38 
 39 - (void)dealloc
 40 {
 41     NSLog(@"%d岁的人被销毁了", _age);
 42     
 43     [super dealloc];
 44 }
 45 @end
 46 
 47 
 48 
 49 
 50 #import "Person.h"
 51 
 52 @interface GoodPerson : Person
 53 
 54 @property (nonatomic, assign) int money;
 55 
 56 @end
 57 #import "GoodPerson.h"
 58 
 59 @implementation GoodPerson
 60 
 61 @end
 62 
 63 
 64 
 65 #import <Foundation/Foundation.h>
 66 #import "Person.h"
 67 #import "GoodPerson.h"
 68 
 69 /*
 70  1.系统自带的方法里面没有包含alloc、new、copy,说明返回的对象都是autorelease的
 71  
 72  2.开发中经常会提供一些类方法,快速创建一个已经autorelease过的对象
 73  1> 创建对象时不要直接用类名,一般用self
 74  + (id)person
 75  {
 76     return [[[self alloc] init] autorelease];
 77  }
 78  */
 79 
 80 int main()
 81 {
 82     @autoreleasepool {
 83         Person *p = [Person personWithAge:100];
 84         
 85         
 86         GoodPerson *p2 = [GoodPerson personWithAge:10];
 87         
 88         p2.money = 100;
 89     }
 90     return 0;
 91 }
 92 
 93 void test()
 94 {
 95     Person *p = [[Person alloc] init];
 96     
 97     p.age = 200;
 98     
 99     [p release];
100     
101     //102     // Student com.mj.test
103     // Student com.mj.test2
104     
105     // MJStudent
106     
107     // SBStudent
108     
109     // Next Step
110     
111     
112     @autoreleasepool
113     {
114         //        Person *p2 = [Person person];
115         //
116         //        p2.age = 100;
117         
118         Person *p2 = [Person personWithAge:100];
119         
120         //        Person *p2 = [[[Person alloc] init] autorelease];
121         //
122         //        p2.age = 300;
123         
124         
125         NSString *str = @"123123";
126         
127         NSString *str2 = [NSString stringWithFormat:@"age is %d", 10];
128         
129         NSNumber *num = [[NSNumber alloc] initWithInt:10];
130         [num release];
131         
132         
133         NSNumber *num2 = [NSNumber numberWithInt:100];
134     }
135 }
原文地址:https://www.cnblogs.com/oc-bowen/p/5053746.html