OC4_内存管理法则

//
//  Dog.h
//  OC4_内存管理法则
//
//  Created by zhangxueming on 15/6/18.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject

@end
//
//  Dog.m
//  OC4_内存管理法则
//
//  Created by zhangxueming on 15/6/18.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "Dog.h"

@implementation Dog

@end
//
//  main.m
//  OC4_内存管理法则
//
//  Created by zhangxueming on 15/6/18.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"
//对于引用计数来说,有一套内存管理的黄金法则:
//The basic rule to apply is everything that increases the reference counter with alloc, [mutable]copy[withZone:] or retain is in charge of the corresponding [auto]release.
//如果对一个对象使用了alloc、[mutable]copy、retain,那么你必须使用相应的release或者autorelease。
//通俗一点的说法就是谁污染谁治理,谁杀的谁埋。
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Dog *xiaoHei = [[Dog alloc] init];//1
        Dog *xiaoBai = [xiaoHei retain];
        Dog *xiaoHui = [xiaoHei copy];//copy用于字符串,不能用于对象,所有这里报错.
        
        [xiaoHei release];
        [xiaoBai release];
        [xiaoHui autorelease];
    }
    return 0;
}
原文地址:https://www.cnblogs.com/0515offer/p/4586950.html