内存管理
对象所有权政策 Object Onwership Policy
任何一个对象都有一定数量的所有者,当一个对象有至少一个所有者时,这个对象就会存在,如果一个对象没有所有者,也就是一个没有任何其他对象需要用这个对象,则这个对象就被系统销毁 ->调用dealloc方法
……
autorelease / NSAutoreleasePool
调用autorelease方法可在一段时间之后撤销所有权。
实质上,调用autorelease方法时,该对象会进入到一个自动释放池中。
什么时候撤销所有权?:当这个自动释放池接收到drain的消息时。
当一个自动释放池接收到drain消息时
1. 会调用池中所有对象的release方法。
2. 如果一个对象被两次加进同一个池中,则该对象此事会被池调用2次release.
3. 调用自动释放池的drain方法如同调用release方法,但一般都会写前者。
自动释放池可以被创建多个,如下:
NSAutoreleasePool pool = [[NSAutoreleasePool alloc] init]; //比较旧的方法
所有的自动释放池都会被放入到一个栈里。
属性访问器
property / synthesize / dot syntax
@property (属性的属性/Attribute of property) 实例变量类型 实例变量;
//
// TRComputer.h
// day04-1
//
//
#import <Foundation/Foundation.h>
#import "TRCpu.h"
#import "TRMem.h"
@interface TRComputer : NSObject
@property(nonatomic, retain) TRCpu* cpu;
@property(nonatomic, retain) TRMem* mem;
-(void)playGame;
-(id)initWithCpu:(TRCpu*)cpu andMem:(TRMem*)mem;
+(id)computerWithCpu:(TRCpu*)cpu andMem:(TRMem*)mem;
@end
//
// TRComputer.m
// day04-1
//
//
#import "TRComputer.h"
@implementation TRComputer
-(id)initWithCpu:(TRCpu *)cpu andMem:(TRMem *)mem{
if (self = [super init]) {
self.cpu = cpu;
self.mem = mem;
}
return self;
}
+(id)computerWithCpu:(TRCpu *)cpu andMem:(TRMem *)mem{
return [[[TRComputer alloc]initWithCpu:cpu andMem:mem]autorelease];
}
-(void)playGame{
NSLog(@"playGame CPU: %dGHZ Mem: %dGB size", self.cpu.hz, self.mem.size);
}
-(void)dealloc{
NSLog(@"computer cpu:%dHZ mem:%dGB销毁了", self.cpu.hz, self.mem.size);
[self.cpu release];
[self.mem release];
[super dealloc];
}
@end
//
// TRCpu.h
// day04-1
//
//
#import <Foundation/Foundation.h>
@interface TRCpu : NSObject
@property(nonatomic, assign) int hz;
-(id)initWithHz:(int)hz;
+(id)cpuWithHz:(int)hz;
@end
//
// TRCpu.m
// day04-1
//
// Created by tarena on 14-3-20.
// Copyright (c) 2014年 tarena. All rights reserved.
//
#import "TRCpu.h"
@implementation TRCpu
-(id)initWithHz:(int)hz{
if (self = [super init]) {
self.hz = hz;
}
return self;
}
+(id)cpuWithHz:(int)hz{
return [[[TRCpu alloc]initWithHz:hz]autorelease];
}
-(void)dealloc{
NSLog(@"cpu %d销毁了", self.hz);
[super dealloc];
}
@end
//
// TRMem.h
// day04-1
//
// Created by tarena on 14-3-20.
// Copyright (c) 2014年 tarena. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TRMem : NSObject
@property(nonatomic, assign)int size;
-(id)initWithSize:(int)size;
+(id)memWithSize:(int)size;
@end
//
// TRMem.m
// day04-1
//
// Created by tarena on 14-3-20.
// Copyright (c) 2014年 tarena. All rights reserved.
//
#import "TRMem.h"
@implementation TRMem
-(id)initWithSize:(int)size{
if (self = [super init]) {
self.size = size;
}
return self;
}
+(id)memWithSize:(int)size{
return [[[TRMem alloc]initWithSize:size]autorelease];
}
-(void)dealloc{
NSLog(@"mem %d销毁了", self.size);
[super dealloc];
}
@end
//
// main.m
// day04-1
//
// Created by tarena on 14-3-20.
// Copyright (c) 2014年 tarena. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "TRComputer.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
TRCpu* cpu1 = [TRCpu cpuWithHz:1];
TRMem* mem1 = [TRMem memWithSize:2];
TRComputer* computer = [TRComputer computerWithCpu:cpu1 andMem:mem1];
computer.cpu = cpu1;
computer.mem = mem1;
[computer playGame];
TRCpu* cpu2 = [TRCpu cpuWithHz:2];
TRMem* mem2 = [TRMem memWithSize:8];
computer.cpu = cpu2;
computer.mem = mem2;
[computer playGame];
}
return 0;
}