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;
}

 

转载于:https://www.cnblogs.com/liuwj/p/6899797.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值