黑马程序员_set方法内存管理



黑马程序员_set方法内存管理

内存管理代码规范:
1.只要调用alloc,必须有release(autorelease)
  如果对象不是通过alloc产生的,就不需要release

2.set方法代码规范:
  * 基本数据类型:直接赋值
    - (void)setAge:(int)age
    {
     _age = age;
    }

  * OC对象类型
- (void)setCar:(Car *)car
{
 // 1.先判断是不是新传进来的对象
 if ( car != _car )
 {
  // 2.对旧对象做一次release
  [_car release];
 
  // 3.对新对象组一次retain; 并赋值给_car
  _car = [car retain];
 }
}

3.dealloc方法的代码规范
  * 一定要调用[super dealloc]; 一定要放到最后面。
 
  * 对当前对象(self)所拥有的其他对象做一次release
- (void)dealloc
{
 [_car release];

 [super dealloc];
}


#import 

/****************** Person ******************/
@interface Person : NSObject
{
	Car *_car;
	int _age;
}

- (void)setAge:(int)age;
- (int)age;

- (void)setCar:(Car *)car;
- (Car *)car;

@end

@implementation Person

- (void)setAge:(int)age
{
	// 基本数据类型是不需要管理内存的
	_age = age;
}

- (int)age
{
	return _age;
}

- (void)setCar:(Car *)car
{
	// 对当前正在使用的车(旧车)做一次release
	if ( car != _car )
	{
		[_car release];	// 第一次赋值时 [nil release]什么也不做

		// 对新车做一次retain操作
		_car = [car retain];
	}
}

- (Car *)car
{
	return _car;
}

- (void)dealloc
{
	// 当Person对象回收了,代表不用Car对象了
	// 对Car对象做一次release操作
	[_car release];

	NSLog (@"Person --- -dealloc: age = %d", _age);
	
	[super dealloc];
}

@end


/****************** Car ******************/
@interface Car : NSObject
{
	int _speed;
}

- (void)setSpeed:(int)speed;
- (int)speed;

@end

@implementation Car

- (void)setSpeed:(int)speed
{
	_speed = speed;
}

- (int)speed
{
	return _speed;
}

- (void)dealloc
{
	/*
 	 * _speed	: 直接访问成员变量
	 * self->speed	: 直接访问成员变量
	 * self.speed	: 通过get方法访问成员变量
	 * [self speed]	: 通过get方法访问成员变量
	 */

	NSLog (@"Car --- -dealloc: speed = %d", _speed);	
	// _speed == self.speed == self->_speed;

	[super dealloc];
}

@end


/****************** Dog ******************/
@interface Dog : NSObject

@end

@implementation Dog

@end


/****************** Student ******************/
@interface Student : NSObject
{
	int _no;
	NSString *_name;
	car *_car;
	Dog *_dog;
}

- (void)setNo:(int)no;
- (int)no;

- (void)setName:(NSString *)name;
- (NSStirng *)name;

- (void)setCar:(Car *)car;
- (Car *)car;

- (void)setDog:(Dog *)dog;
- (Dog *)dog;

@end

@implementation Student

- (void)setNo:(int)no
{
	_no = no;
}

- (int)no
{
	return _no;
}

- (void)setName:(NSString *)name
{
	if ( name != _name )
	{
		[_name release];
		_name = [name retain];
	}
}

- (NSStirng *)name
{
	return _name;
}

- (void)setCar:(Car *)car
{
	if ( car != _car )
	{
		[_car release];
		_car = [car retain];
	}
}
- (Car *)car
{
	return _car;
}

- (void)setDog:(Dog *)dog
{
	if ( dog = _dog )
	{
		[_dog release];
		_dog = [dog retain];
	}
}
- (Dog *)dog
{
	return _dog;
}

- (void)dealloc
{
	[_name release];
	[_dog release];
	[_car release];

	[super dealloc];
}

@end

/****************** main ******************/
int main()
{
	// stu-1
	Student *stu = [[Student alloc] init];

	// car-2
	// 这行代码有内存泄漏
	// stu.car = [[Car alloc] init];

	// stu-0; car-1
	[stu release];

	// 这行代码有内存泄漏
	// [Car alloc] init].speed = 100;

	return 0;
}

void test3()
{
	Student *stu = [[Student alloc] init];
	
	Car *c = [[Car alloc] init];
	stu.car = c;

	Dog *d = [[Dog alloc] init];
	stu.dog = d;

	
	stu.name = @"jack";
	
	[d release];
	[c release];
	[stu release];	
}

void test2()
{
	Person *p1 = [[Person alloc] init];
	p.age = 20;

	Car *c1 = [[Car alloc] init];
	c1.speed = 100;

	p1.car = c1;

	Car *c2 = [[Car alloc] init];
	c2.speed = 200;

	[c2 release];
	[c1 release];	
	[p1 release];
}

void test1()
{
	// p-1
	Person *p = [[Person alloc] init];
	p.age = 20;

	// c1-1
	Car *c1 = [[Car alloc] init];
	c1.speed = 250;

	// c1-2
	p.car = c1;

	// c1-1
	[c1 release];

	// c1-0 僵尸对象
	// p.car = c1;
	
	[p release];
}

void test()
{
	// p-1
	Person *p = [[Person alloc] init];
	p.age = 20;

	// c1-1
	Car *c1 = [[Car alloc] init];
	c1.speed = 250;

	// p想拥有c1
	// c1-2
	p.car = c1;	// [p setCar:c1];

	// c2-1
	Car *c2 = [[Car alloc] init];
	c2.speed = 300;

	// p将车换成了c2
	// c2-2; c1-1;
	p.car = c2;
	
	// c2-1
	[c2 release];

	// c1-1
	[c1 release];

	// p-0; c2-0; c1-0;	
	[p release];
}

你可能感兴趣的:(黑马程序员_set方法内存管理)