多对象的内存管理
1>当你想占有某个对象时,就应该让对象的引用计数器+1(让对象做一次retain操作)
2>当你不占有某个对象时,让对象的引用计数器-1(让对象做一次release操作)
3>谁retain,谁release
4>谁alloc,谁release
5>当引用计数为0 ,就会调用对象的dealloc
6>[super dealloc];写到最后
1 #import <Foundation/Foundation.h> 2 #import "Book.h" 3 #import "Person.h" 4 int main(int argc, const char * argv[]) { 5 6 // 当引用计数为0 ,就会调用对象的dealloc 7 8 // b=1 b=1 9 Book *b = [[Book alloc]init]; 10 // p1=1 p1=1 11 Person *p1 = [[Person alloc]init]; 12 // p1占有了 13 // p1 =1,b=2 p1 =1,b=2 14 [p1 setBook:b]; 15 // [b retain]; 16 // alloc +1,retain+1 17 // p1 =0,b=0 p1=0 ,b=1 18 [p1 release]; 19 // b=1 b=0 20 [b release]; 21 // b = nil; 22 // [b release]; 23 24 return 0; 25 }
1 #import <Foundation/Foundation.h> 2 3 @interface Book : NSObject 4 { 5 6 int _price; 7 8 } 9 //@property int price; 10 11 - (void)setPrice:(int)price; 12 - (int)price; 13 @end
1 @implementation Book 2 3 - (void)setPrice:(int)price{ 4 5 _price = price; 6 7 } 8 - (int)price{ 9 10 return _price; 11 12 } 13 14 - (void)dealloc{ 15 16 NSLog(@"BOOK对象被销毁"); 17 [super dealloc]; 18 19 } 20 @end
1 #import <Foundation/Foundation.h> 2 #import "Book.h" 3 @interface Person : NSObject 4 { 5 6 Book *_book; 7 8 } 9 10 - (void)setBook:(Book *)book; 11 - (Book *)book; 12 @end
1 #import "Person.h" 2 3 @implementation Person 4 5 - (void)setBook:(Book *)book{ 6 // _book = book; 7 _book =[book retain]; 8 9 } 10 - (Book *)book{ 11 12 return _book; 13 14 } 15 16 - (void)dealloc{ 17 18 [_book release]; // 人类被销毁,Book也被销毁 19 NSLog(@"Person对象被销毁"); 20 [super dealloc]; 21 22 } 23 @end
_____________________________________________________set方法的内存管理 _____________________________________________________
内存管理代码:
1>只要调用了alloc就必须要有release(autorelease)
对象不是通过alloc产生的,就不要release
2>set代码规范:基本数据类型直接赋值
3>dealloc代码规范:一定要调用[super dealloc]并且当前一定要对象所拥有的对象做一次release
1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3 #import "Person.h" 4 int main(int argc, const char * argv[]) { 5 Person *p = [[Person alloc]init]; 6 Car *c1 = [[Car alloc]init]; 7 c1.speed = 100; 8 p.age = 18; 9 // p拥有c1 10 11 // 僵尸对象,当某个对象的计数器为0,对它retain是不对的,防止错误:在set方法中加一个判断 12 p.car = c1; 13 // p.car = c1; 14 // p.car = c1; 15 // [c1 release]; 16 [c1 release]; 17 Car *c2 = [[Car alloc]init]; 18 c2.speed = 200; 19 p.car = c2; 20 21 [c2 release]; 22 [p release]; 23 return 0; 24 }
1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3 @interface Person : NSObject 4 { 5 6 Car *_car; 7 int _age; 8 } 9 10 - (void)setCar:(Car *)car; 11 - (int)car; 12 13 - (void)setAge:(int)age; 14 - (int)age; 15 @end
1 #import "Person.h" 2 3 @implementation Person 4 - (void)setCar:(Car *)car{ 5 if (car !=_car){ // 判断传入的值是不是原来那个 6 // 对旧车做一次release 7 [_car release]; 8 // 对新车做一次retain 9 _car = [car retain]; 10 11 } 12 // _car = [car retain]; 13 } 14 - (int)car{ 15 16 return _car; 17 18 } 19 20 21 - (void)setAge:(int)age{ 22 // 基本数据类型直接赋值 23 _age = age; 24 25 } 26 - (int)age{ 27 28 return _age; 29 30 } 31 - (void)dealloc{ 32 NSLog(@"person对象呗销毁了"); 33 [_car release]; 34 35 [super dealloc]; 36 37 } 38 @end
1 #import <Foundation/Foundation.h> 2 3 @interface Car : NSObject 4 { 5 6 int _speed; 7 8 } 9 - (void)setSpeed:(int)speed; 10 - (int)speed; 11 @end
1 #import "Car.h" 2 3 @implementation Car 4 - (void)setSpeed:(int)speed{ 5 6 _speed = speed; 7 } 8 - (int)speed{ 9 10 return _speed; 11 12 } 13 - (void)dealloc{ 14 // self->_speed 直接访问成员变量 15 // _speed 直接访问成员变量 16 // [self speed] get方法 17 // self.speed get方法 18 NSLog(@"速度为%d的Car对象被销毁了",_speed); 19 [super dealloc]; 20 } 21 @end