microleo的个人博客

iOS set方法内存管理

内存管理代码规范:

1.只要调用了alloc,必须有release(autorelease) 对象不是通过alloc产生的,就不需要release

2.set方法的代码规范 基本数据类型:直接复制

- (void)setAge:(int)age
{
_age = age;
}

2.OC对象类型

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

3.dealloc方法的代码规范

1> 一定要[super dealloc],而且放到最后面
2> 对self(当前)所拥有的其他对象做一次release
- (void)dealloc
{
[_car release];
[super dealloc];
}
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Person.h"
#import "Student.h"
#import "Dog.h"
// 速度为100的车子:
// 速度为200的车子:p1->_car
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;
NSString *s = @"Jack";
stu.name = s;
[d release];
[c release];
[stu release];
}
void test2()
{
Person *p1 = [[Person alloc] init];
p1.age = 20;
// c1 - 1
Car *c1 = [[Car alloc] init];
c1.speed = 100;
// c1 - 2
p1.car = c1;
// c1 - 1
[c1 release];
Car *c2 = [[Car alloc] init];
c2.speed = 200;
// c1 - 0
p1.car = c2;
[c2 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];
p.car = c1;
p.car = c1;
p.car = c1;
p.car = c1;
p.car = c1;
p.car = c1;
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
// c1-1
// c2-2
p.car = c2;
// c2-1
[c2 release];
// c1-0
[c1 release];
// p-0 c2-0
[p release];
}
@interface Car : NSObject
{
int _speed;
}
- (void)setSpeed:(int)speed;
- (int)speed;
@end
#import "Car.h"
@implementation Car
- (void)setSpeed:(int)speed
{
_speed = speed;
}
- (int)speed
{
return _speed;
}
- (void)dealloc
{
/*
_speed :直接访问成员变量
self->_speed :直接访问成员变量
self.speed : get方法
[self speed] : get方法
*/
NSLog(@"速度为%d的Car对象被回收了", _speed);
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "Car.h"
@interface Person : NSObject
{
Car *_car;
int _age;
}
- (void)setAge:(int)age;
- (int)age;
- (void)setCar:(Car *)car;
- (Car *)car;
@end
#import "Person.h"
// _car -> c1 0
@implementation Person
- (void)setCar:(Car *)car
{
if (car != _car)
{
// 对当前正在使用的车(旧车)做一次release
[_car release];
// 对新车做一次retain操作
_car = [car retain];
}
}
- (Car *)car
{
return _car;
}
- (void)setAge:(int)age
{ // 基本数据类型不需要管理内存
_age = age;
}
- (int)age
{
return _age;
}
- (void)dealloc
{
// 当人不在了,代表不用车了
// 对车做一次release操作
[_car release];
NSLog(@"%d岁的Person对象被回收了", _age);
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Dog.h"
@interface Student : NSObject
{
int _no;
NSString *_name;
Car *_car;
Dog *_dog;
}
- (void)setNo:(int)no;
- (int)no;
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setCar:(Car *)car;
- (Car *)car;
- (void)setDog:(Dog *)dog;
- (Dog *)dog;
@end
#import "Student.h"
@implementation Student
- (void)setNo:(int)no
{
_no = no;
}
- (int)no
{
return _no;
}
- (void)setName:(NSString *)name
{
if ( name != _name )
{
[_name release];
_name = [name retain];
}
}
- (NSString *)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];
[_car release];
[_dog release];
[super dealloc];
}
@end