• 引用计数(retainCount)


    ClassA.h:

    #import <Foundation/Foundation.h>
    @interface ClassA:NSObject
    {
        NSString *name;
    }
    -(void)setName:(NSString*)s;
    -(NSString*)name;
    -(void)dealloc;
    
    @end

    ClassA.m:

    #import "ClassA.h"
    @implementation ClassA
    -(void)setName:(NSString *)s
    {
        name=s;
        [name retain];
    }
    -(NSString*)name
    {
        return name;
    }
    -(void)dealloc
    {
        NSLog(@"delloc coling!");
        [name release];
        [super dealloc];
    }
    @end

    main.m:

    #import <Foundation/Foundation.h>
    #import "ClassA.h"
    int main(int argc,const char *argv[])
    {
        NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
        NSMutableString *str1=[NSMutableString stringWithString:@"Sam"];
        ClassA *classA=[[ClassA alloc]init];
        NSLog(@"Str1 count: %x",[str1 retainCount]);
        
        [classA setName:str1];
        NSLog(@"str1 count:%x",[str1 retainCount]);
        
        [str1 release];
        NSLog(@"str1 count:%x",[str1 retainCount]);
        [classA release];
        [pool drain];
        return  0;
    }

    console log:

    2013-09-22 16:35:50.886 demo[1028:303] Str1 count: 1

    2013-09-22 16:35:50.890 demo[1028:303] str1 count:2

    2013-09-22 16:35:50.891 demo[1028:303] str1 count:1

    2013-09-22 16:35:50.892 demo[1028:303] delloc coling!

  • 相关阅读:
    gearman管理
    php运行方式
    gearman mysql持久化
    gearman安装及初次使用
    消息队列各种比较
    IOC
    post提交/文件上传服务器修改
    protobuf php
    thrift 安装介绍
    qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题
  • 原文地址:https://www.cnblogs.com/wangwenfei/p/retaionCount.html
Copyright © 2020-2023  润新知