NSObject简化版alloc:

struct obj_layout {
NSUInteger retained;
}; + (id)alloc { int size = sizeof(struct obj_layout) + 对象大小;
struct obj_layout *p = (struct obj_layout *)calloc(, size);
return (id)(p + );
}

(将引用计数保存在对象占用内存块头部的变量中是GNUstep的实现。而苹果的实现,则是保存在引用计数表中。引用计数表可以用hash表实现,表键值为内存块地址的散列值。)

  retain方法使retained变量加1;

  release方法使retained变量减1;

  retainCount方法返回retained变量 + 1。

  autorelease方法的本质就是调用NSAutoreleasePool对象的addObject类方法。

- (id)allocObject {

    id obj = [[NSObject alloc] init];
return obj;
} - (id)object { id obj = [[NSObject alloc] init];
[obj autorelease];
return obj;
} id obj1 = [obj0 allocObject];
id obj2 = [obj0 object];

obj1持有对象,obj2不持有对象。

(可以认为object方法对应一个NSAutoreleasePool。方法结束时,NSAutoreleasePool被废弃,pool内obj的release方法被调用。)

在ARC下,因为变量obj1和obj2都是强引用,所以都强持有新生成的对象。

强引用存在循环引用问题,可以使用弱引用来避免。

__weak修饰符注意点

弱引用失效时,会被置为nil。

(__unsafe_unretained失效时,不会被置为nil。)

id __weak obj = [[NSObject alloc] init];
// 自己生成并持有的对象不能继续为自己所有,所以生成的对象会立即被释放。__unsafe_unretained修饰符也同样。

__ autoreleasing用于对(id *)类型的参数传递.

Automatic Reference Counting的更多相关文章

  1. JSONKit does not support Objective-C Automatic Reference Counting(ARC) / ARC forbids Objective-C objects in struct

    当我们在使用JSONKit处理数据时,直接将文件拉进项目往往会报这两个错“JSONKit   does not support Objective-C Automatic Reference Coun ...

  2. iOS开发 JSonKit does not support Objective-C Automatic Reference Counting(ARC)

    有使用JSonKit的朋友,如果遇到“JSonKit does not support Objective-C Automatic Reference Counting(ARC)”这种情况,可参照如下 ...

  3. [转]关于NSAutoreleasePool' is unavailable: not available in automatic reference counting mode的解决方法

    转载地址:http://blog.csdn.net/xbl1986/article/details/7216668 Xcode是Version 4.2 Build 4D151a 根据Objective ...

  4. 错误解决:release' is unavailable: not available in automatic reference counting mode

    解决办法: You need to turn off Automatic Reference Counting. You do this by clicking on your project in ...

  5. not available in automatic reference counting mode

    UncaughtExceptionHandler.m:156:47: 'autorelease' is unavailable: not available in automatic referenc ...

  6. error: 'release' is unavailable: not available in automatic reference counting,该怎么解决

    编译出现错误: 'release' is unavailable: not available in automatic reference counting mode.. 解决办法: You nee ...

  7. xcode禁用ARC(Automatic Reference Counting)

    Automatic Reference Counting,自动引用计数,即ARC,可以说是WWDC2011和iOS5所引入的最大的变革和最激动人心的变化.ARC是新的LLVM 3.0编译器的一项特性, ...

  8. ARC(Automatic Reference Counting )技术概述

    此文章由Tom翻译,首发于csdn的blog 转自:http://blog.csdn.net/nicktang/article/details/6792972 Automatic Reference ...

  9. [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode

    转载地址:http://choijing.iteye.com/blog/1860761   后来发现是编译选项的问题:   1.点击工程名 打开编译选项 2.在编译选项中,选择Bulid Settin ...

  10. Welcome-to-Swift-16自动引用计数(Automatic Reference Counting)

    Swift使用自动引用计数(ARC)来跟踪并管理应用使用的内存.大部分情况下,这意味着在Swift语言中,内存管理"仍然工作",不需要自己去考虑内存管理的事情.当实例不再被使用时, ...

随机推荐

  1. lr具体使用步骤概述

    lr具体使用 1 无工具情况下的性能测试 2性能测试工具LoadRunner的工作原理 3 VuGen应用介绍 4 协议的类型及选择方法 5 脚本的创建过程 6 脚本的参数化 7 调试技术 8 Con ...

  2. C# windows服务没有RunInstallerAttribute.Yes的公共安装程序

    1.在视图状态 右键添加ServiceInstaller及ServiceProcessInstaller两个控件; 2.将serviceProcessInstaller类的Account属性改为 Lo ...

  3. sudo -s 命令 [oh-my-zsh] 提示检测到不安全目录

    运行sudo -s 命令时,[oh-my-zsh] 冒出下面一大堆提示: [oh-my-zsh] Insecure completion-dependent directories detected: ...

  4. JS 获取浏览器

    function getInfo() { var s = ""; s = " 网页可见区域宽:" document.body.clientWidth; s = ...

  5. 苹果登录服务端JWT算法验证-PHP

    验证参数 可用的验证参数有 userID.authorizationCode.identityToken,需要iOS客户端传过来 验证方式 苹果登录验证可以选择两种验证方式 具体可参考这篇文章 htt ...

  6. uniqid用法

    uniqid():妙用就是以当前时间微妙为单位,返回的唯一ID 我们可以用到密码加密和接口加密的功能上,比如 $salt = substr(uniqid(rand()), -6);//截取倒数6位$p ...

  7. 20199326《Linux内核原理与分析》第十一周作业

    Shellsock攻击实验 实验背景 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发. Shellshock,又称Bash ...

  8. linux抓包的实现

    工具: wireshark tcpdump 在这里仅仅介绍后者: 在网络问题的调试中,tcpdump应该说是一个必不可少的工具,和大部分linux下优秀工具一样,它的特点就是简单而强大. 默认情况下, ...

  9. vue2.x学习笔记(二十六)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12682137.html. 单文件组件 介绍 在很多的vue项目中,我们都是使用[Vue.component]来定义 ...

  10. 记django从1.11.7升级到2.0.1

    第一步:升级django之后记录下django等其他相关依赖包的版本号. 在terminal中输入 pip freeze, 获取所有包的版本号.为了在升级不成功后可以回到低版本. 第二步:卸载再重装d ...