Object-c学习之路二(oc内存管理黄金法则1)

今天看了看oc的内存管理,自己管理内存不能随便的release和retain 法则会出现野指针等错误。下面以人和读书的例子做练习。

1.主函数

 

//

//  main.m

//  MemoryManagement

//

//  Created by WildCat on 13-7-23.

//  Copyright (c) 2013年 wildcat. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "Student.h"

#import "Book.h"

void test1(){



    

    Student *stu=[[Student alloc] init];

    

    [stu retain];

    

    NSLog(@"stu 的计算器为:%zi",[stu retainCount]);//2

    [stu release];//1

    [stu release];//0

    

}

#pragma mark 添加书

void addBook(Student *stu){

    Book *book=[[Book alloc] initWithPrice:3.5];

    stu.book=book;

    [book release];

    

    Book *book2=[[Book alloc] initWithPrice:4.5];

    stu.book=book2;

    [book2 release];



}

#pragma mark 读书

void readBooks(Student *stu){

    [stu readBook];



}

#pragma mark 主函数

int main(int argc, const char * argv[])

{



    @autoreleasepool {



        Student *stu=[[Student alloc] initWithAge:22];

        Student *stu1=[[Student alloc] initWithAge:20];



        addBook(stu1);

        readBooks(stu1);

        

        addBook(stu);

        readBooks(stu);

        [stu release];//多写[stu release]会出现野指针错误,[nil release]则不会

        [stu1 release];

        

      

    }

    return 0;

}

 

2.Student函数

 

//  Student.h

//  MemoryManagement

//

//  Created by WildCat on 13-7-23.

//  Copyright (c) 2013年 wildcat. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "Book.h"

@interface Student : NSObject{

    int _age;

    Book *_book;



}

@property int age;

@property Book *book;



-(id)initWithAge:(int)age;

-(void) readBook;

@end

 

//

//  Student.m

//  MemoryManagement

//

//  Created by WildCat on 13-7-23.

//  Copyright (c) 2013年 wildcat. All rights reserved.

//



#import "Student.h"



@implementation Student

@synthesize age=_age;

-(Book *)getBook{

    return _book;



}

-(void)setBook:(Book *)book{

    if (_book!=book){

        //先释放旧的

        [_book release];

        //再retain 新的

        _book=[book retain];

    }

    

   }





//重写dealloc方法,当对象的计数器是1时自动调用该方法

#pragma mark - 静态方法

#pragma mark 回收对象

- (void)dealloc

{

    [_book release];

    NSLog(@"Student %i,内存被释放。",_age);

    [super dealloc];

}





#pragma mark - 公共方法

#pragma mark 读书

-(void)readBook{



    NSLog(@"当前读的书为:%f",_book.price);



}

#pragma mark 构造函数

-(id)initWithAge:(int)age{

    if(self=[super init]){

        _age=age;

    }

    

    return self;

}



@end


 

3.Book函数

 

//  Book.h

//  MemoryManagement

//

//  Created by WildCat on 13-7-23.

//  Copyright (c) 2013年 wildcat. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface Book : NSObject{

    float _price;

}

@property float price;





-(id) initWithPrice:(float)price;



@end

 

//  Book.m

//  MemoryManagement

//

//  Created by WildCat on 13-7-23.

//  Copyright (c) 2013年 wildcat. All rights reserved.

//



#import "Book.h"



@implementation Book

@synthesize price=_price;







#pragma mark 注销函数

-(void)dealloc{



    NSLog(@"Book %f ,被销毁。",_price);

    [super dealloc];

}

#pragma mark 构造函数

-(id) initWithPrice:(float)price{

      if (self=[super init]){

       _price=price;

    }

    return self;

}

@end


Object-c学习之路二(oc内存管理黄金法则1) Object-c学习之路二(oc内存管理黄金法则1)

 










你可能感兴趣的:(object)