//
// main.m
// oc04_内存管理
//
// Created by Vision on 14-9-16.
// Copyright (c) 2014年 Vision. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "House.h"
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//野指针
//错误1:指向访问受限的内存区域
int *p = 0x0001;
NSLog(@"%d", *p);
//错误2:指向已经回收的内存区域
int *q = malloc(sizeof(int));
*q = 4;
free(q);
NSLog(@"%d",*q);
*q = 5;
NSLog(@"%d",*q);
}
@autoreleasepool {
//1.alloc
House *xiaoGuiZiHouse = [[House alloc]init];
//2.retain
NSLog(@"%lu", [xiaoGuiZiHouse retainCount]);
House *jiaHongHouse = [xiaoGuiZiHouse retain];
NSLog(@"%lu", [xiaoGuiZiHouse retainCount]);
NSLog(@"%lu", [jiaHongHouse retainCount]);
House *zijieHouse = [xiaoGuiZiHouse retain];
NSLog(@"%lu", [zijieHouse retainCount]);
//3.release
[zijieHouse release];
zijieHouse = nil;//防止野指针
[jiaHongHouse release];
jiaHongHouse = nil;
[xiaoGuiZiHouse autorelease];
NSLog(@"%lu", [xiaoGuiZiHouse retainCount]);
// 1 -> 0过程观察不到
//[nil retainCount] == 0
}
House *house = [[House alloc]init];
@autoreleasepool {
[house retain];
NSLog(@"%lu",[house retainCount]);
[house autorelease];//引用计数暂时不变
NSLog(@"%lu",[house retainCount]);
}
//引用计数 - 1
NSLog(@"%lu",[house retainCount]);
@autoreleasepool {
House *hunFang = [[[House alloc]init] autorelease];// 防止忘记release ,或不确定何时release
}
@autoreleasepool {
Person *erMu = [[Person alloc]initWithName:@"二木"];
Person *yuYuan = [[Person alloc]initWithName:@"余元"];
Person *dongHai = [[Person alloc]initWithName:@"东海"];
[erMu autorelease];
[yuYuan autorelease];
[dongHai autorelease];
Person *liHongXun = [erMu retain];
[liHongXun release];
Person *guiChua = [[Person alloc]initWithName:@"鬼船"];
Person *guiChuanClone = [guiChua copy];
[guiChuanClone release];
Person *yaNan = [Person personWithName:@"亚楠"];
//注意:便利构造器创建的对象无需额外release
}
return 0;
}
<pre name="code" class="objc">//
// House.h
// oc04_内存管理
//
// Created by Vision on 14-9-16.
// Copyright (c) 2014年 Vision. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface House : NSObject
@end
//
// House.m
// oc04_内存管理
//
// Created by Vision on 14-9-16.
// Copyright (c) 2014年 Vision. All rights reserved.
//
#import "House.h"
@implementation House
@end
//
// Person.h
// oc04_内存管理
//
// Created by Vision on 14-9-16.
// Copyright (c) 2014年 Vision. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCopying>
{
NSString *_name;
}
- (id)initWithName:(NSString *)name;
- (id)copyWithZone:(NSZone *)zone;
+ (id)personWithName:(NSString *)name;
@end
//
// Person.m
// oc04_内存管理
//
// Created by Vision on 14-9-16.
// Copyright (c) 2014年 Vision. All rights reserved.
//
#import "Person.h"
@implementation Person
- (id)initWithName:(NSString *)name
{
self = [super init];
if (self) {
_name = name;
}
return self;
}
- (void)dealloc
{
NSLog(@"%@被释放了个屁的",
_name);
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
Person *p = [[Person allocWithZone:zone]initWithName:[NSString stringWithFormat:@"%@的克隆人",
_name]];
return p;
}
+ (id)personWithName:(NSString *)name
{
return [[[Person alloc]initWithName:name]autorelease];
}
@end