//
// Dog.h
// OC3_dealloc
//
// Created by zhangxueming on 15/6/18.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
//
// Dog.m
// OC3_dealloc
//
// Created by zhangxueming on 15/6/18.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import "Dog.h"
@implementation Dog
- (
void)dealloc
{
NSLog(@"dog release!!!");
[super dealloc];
}
@end
//
// main.m
// OC3_dealloc
//
// Created by zhangxueming on 15/6/18.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(
int argc,
const char *
argv[]) {
@autoreleasepool {
Dog *xiaoHei =
[[Dog alloc] init];
Dog *xiaoBai =
[xiaoHei retain];
NSLog(@"retainCount = %li", xiaoBai.retainCount);
Dog *xiaoHui =
[xiaoHei retain];
NSLog(@"retainCount = %li", xiaoHui.retainCount);
[xiaoHei release];
NSLog(@"retainCount = %li", xiaoBai.retainCount);
[xiaoBai release];
NSLog(@"retainCount = %li", xiaoHui.retainCount);
//最后一次release 的时候,retainCount理论值为的0, 自动调用dealloc方法释放对象
[xiaoHui release];
}
return 0;
}
转载于:https://www.cnblogs.com/0515offer/p/4586945.html