zoukankan      html  css  js  c++  java
  • OC 内存管理(retain和release)

     内存管理 retain和release简单使用

    #import "Student.h"
    
    @implementation Student
    @synthesize age = _age; // 在xcode4.5环境下可以省略
    
    - (void)dealloc {
        NSLog(@"%@被销毁了", self);
        
       [super dealloc];
        // 一定要调用super的dealloc方法,而且最好放在最后面调用
    }
    @end
    #import <Foundation/Foundation.h>
    #import "Student.h"
    
    void test() {
        Student *stu = [[Student alloc] init]; // 1
        
        // z代表无符号
        NSLog(@"count:%zi", [stu retainCount]);
        
        [stu retain]; // 2
        
        NSLog(@"count:%zi", [stu retainCount]);
        
        [stu release]; // 1
        
        NSLog(@"count:%zi", [stu retainCount]);
        
        [stu release]; // 0
        
        // [stu release]; // 会发生野指针错误,也就是说访问了不属于你的内存
    }
    
    void test1() {
        // Student对象的计数器永远为1,所以不可能被释放
        [[Student alloc] init].age = 10;
        
        
        [Student new].age = 10;
        
        // 上面的代码都有内存泄露
    }
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
            
        }
        return 0;
    }
    View Code

     

    对象之间的内存管理

    #import "Student.h"
    
    @implementation Student
    
    #pragma mark - 生命周期方法
    #pragma mark 构造方法
    - (id)initWithAge:(int)age {
        if ( self = [super init] ) {
            _age = age;
        }
        
        return self;
    }
    
    #pragma mark 回收对象
    - (void)dealloc {
        // 释放Book对象
        [_book release];
        
        // [self.book release];
        
        NSLog(@"student:%i 被销毁了", _age);
        [super dealloc];
    }
    
    #pragma mark - getter和setter
    // @synthesize book = _book;
    // 如果自己手动实现了getter和setter,xcode就不会自动生成@synthesize
    // 也就不会自动生成_book
    // getter和setter的默认实现
    - (void)setBook:(Book *)book {
        if (_book != book) {
            // 先释放旧的成员变量
            [_book release];
            // 再retain新传进来的对象
            _book = [book retain];
        }
    }
    
    - (Book *)book {
        return _book;
    }
    
    #pragma mark - 公共方法
    #pragma mark 读书
    - (void)readBook {
        NSLog(@"当前读的书是:%f", _book.price);
    }
    
    
    
    //#pragma mark - 私有方法
    //#pragma mark 私有方法1
    //- (void)test1 {
    //
    //
    //}
    //#pragma mark 私有方法2
    //- (void)test2 {
    //    
    //    
    //}
    //#pragma mark 私有方法3
    //- (void)test3 {
    //    
    //    
    //}
    
    @end
    View Code

    @property的参数

    #import <Foundation/Foundation.h>
    
    @class Book;
    @class Card;
    
    @interface Student : NSObject
    
    // 这里的retain代表:在set方法中,release旧值,retain新值
    @property (nonatomic, retain) Book *book;
    
    @property (retain) Card *card;
    
    // readonly代表只生成get方法的声明
    // 默认是readwrite,同时生成get和set方法的声明
    @property (readonly) int age;
    
    // atomic就代表给方法进行加锁,保证线程安全
    @property (atomic) int no;
    
    // nonatomic代表方法不需要考虑线程安全问题
    @property (nonatomic, assign) int no2;
    
    // getter是用来指定get方法的方法名
    @property (nonatomic, getter = isRich) BOOL rich;
    @end

    autorelease

    #import <Foundation/Foundation.h>
    #import "Student.h"
    
    int main(int argc, const char * argv[])
    {
        // @autoreleasepool代表创建一个自动释放池
        @autoreleasepool {
            Student *stu = [[[Student alloc] init] autorelease];
            
            //[stu autorelease];
            
            Student *stu1 = [[[Student alloc] init] autorelease];
            //[stu1 autorelease];
            
            // 这个stu2是自动释放的,不需要释放
            Student *stu2 = [Student student];
            
            // 这个str是自动释放的,不需要释放
            NSString *str = [NSString stringWithFormat:@"age is %i", 10];
            
            
            for (int i = 0; i<1000; i++) {
                [Student student];
            }
        }
        return 0;
    }
  • 相关阅读:
    剑指Offer 28 对称的二叉树
    剑指Offer 54 二叉搜索树的第k大节点
    剑指Offer 41 数据流中的中位数
    剑指Offer 59 队列的最大值
    剑指Offer 12 矩阵中的路径
    剑指Offer 13 机器人的运动范围
    剑指Offer 42 连续子数组的最大和
    一句话总结随机森林
    一句话总结卷积神经网络
    一句话总结K均值算法
  • 原文地址:https://www.cnblogs.com/liuwj/p/6899797.html
Copyright © 2011-2022 走看看