首页 > 其他 > 详细

OC4_内存管理法则

时间:2015-06-18 22:05:08      阅读:156      评论:0      收藏:0      [点我收藏+]
//
//  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;
}

 

OC4_内存管理法则

原文:http://www.cnblogs.com/0515offer/p/4586950.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!