一 概述
上篇文章简单介绍了内存管理set的基本方法,可是依然有不太完善的地方,如给一个类成员通过set方法设置另一个类成员时及多次设置时,如何避免内存泄漏呢?
二 代码实例
2.1 类实例(Car)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| //Car.h #import <Foundation/Foundation.h> @interface Car : NSObject { int _speed; } -(void)setSpeed:(int)speed; -(int)speed;
//Car.m #import "Car.h"
@implementation Car - (void)setSpeed:(int)speed { _speed=speed; } -(int)speed { return _speed; } -(void)dealloc { NSLog(@"速度为%d的Car对象被回收了",self.speed); [super dealloc]; } @end
|
2.2 类实例(Person)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| //Person.h #import <Foundation/Foundation.h> #import "Car.h" @interface Person : NSObject { Car *_car; int _age; } -(void)setCar:(Car*)car; -(Car*)car; -(void)setAge:(int)age; -(int)age; @end
//Person.m #import "Person.h" @implementation Person - (void)setCar:(Car *)car { if(car!=_car) { [_car release]; _car=[car retain]; //_car=car; } } -(Car *)car { return _car; } -(void)setAge:(int)age { _age=age; } -(int)age { return _age; } -(void)dealloc { [_car release]; NSLog(@"Person对象被回收了"); [super dealloc]; } @end
|
2.3 main.m(运行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #import <Foundation/Foundation.h> #import "Person.h" #import "Car.h"
int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... // Person *p=[[Person alloc]init]; // p.age=20; // Car *c1=[[Car alloc]init]; // c1.speed=250; // p.car=c1; // // Car *c2=[[Car alloc]init]; // c2.speed=300; // p.car=c2; // // [c2 release]; // [c1 release]; // [p release]; Person *p=[[Person alloc]init]; p.age=20; Car *c1=[[Car alloc]init]; c1.speed=250; p.car=c1; [c1 release]; p.car=c1; p.car=c1; [p release]; } return 0; }
|