标签:内存管理 计数器 多个对象之间的内存管 objective
/*
1.你想使用(占用)某个对象,就应该让对象的计数器+1(让对象做一次retain操作)
2.你不想再使用(占用)某个对象,就应该让对象的计数器-1(让对象做一次release)
3.谁retain,谁release
4.谁alloc,谁release
*/
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Book.h"
int main()
{
// b-1
Book *b = [[Book alloc] init];
// p-1
Person *p1 = [[Person alloc] init];
//p1想占用b这本书
// b-2
[p1 setBook:b];
// p-0
// b-1
[p1 release];
p1 = nil;
// b-0
[b release];
b = nil;
return 0;
}
@interface Book : NSObject
{
int _price;
}
- (void)setPrice:(int)price;
- (int)price;
@end
#import "Book.h"
@implementation Book
- (void)setPrice:(int)price
{
_price = price;
}
- (int)price
{
return _price;
}
- (void)dealloc
{
NSLog(@"Book对象被回收");
[super dealloc];
}
@end
#import "Book.h"
@interface Person : NSObject
{
Book *_book;
}
- (void)setBook:(Book *)book;
- (Book *)book;
@end
#import "Person.h"
@implementation Person
- (void)setBook:(Book *)book
{
_book = [book retain];
}
- (Book *)book
{
return _book;
}
- (void)dealloc
{
[_book release];
NSLog(@"Person对象被回收");
[super dealloc];
}
@end
//内存管理代码规范:
//1.只要调用了alloc,必须有release(autorelease)
// 对象不是通过alloc产生的,就不需要release
//2.set方法的代码规范
// 1> 基本数据类型:直接复制
- (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];
}
*/
// 速度为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];
}
#import "Car.h"
@interface Person : NSObject
{
Car *_car;
int _age;
}
- (void)setAge:(int)age;
- (int)age;
- (void)setCar:(Car *)car;
- (Car *)car;
@end
@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
@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(@"速度为%d的Car对象被回收了", _speed);
[super dealloc];
}
@end
#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
@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
@interface Dog : NSObject
@end
@implementation Dog
@end
标签:内存管理 计数器 多个对象之间的内存管 objective
原文地址:http://blog.csdn.net/wangzi11322/article/details/45165799