Automatic Reference Counting
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的更多相关文章
- 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 ...
- iOS开发 JSonKit does not support Objective-C Automatic Reference Counting(ARC)
有使用JSonKit的朋友,如果遇到“JSonKit does not support Objective-C Automatic Reference Counting(ARC)”这种情况,可参照如下 ...
- [转]关于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 ...
- 错误解决: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 ...
- not available in automatic reference counting mode
UncaughtExceptionHandler.m:156:47: 'autorelease' is unavailable: not available in automatic referenc ...
- error: 'release' is unavailable: not available in automatic reference counting,该怎么解决
编译出现错误: 'release' is unavailable: not available in automatic reference counting mode.. 解决办法: You nee ...
- xcode禁用ARC(Automatic Reference Counting)
Automatic Reference Counting,自动引用计数,即ARC,可以说是WWDC2011和iOS5所引入的最大的变革和最激动人心的变化.ARC是新的LLVM 3.0编译器的一项特性, ...
- ARC(Automatic Reference Counting )技术概述
此文章由Tom翻译,首发于csdn的blog 转自:http://blog.csdn.net/nicktang/article/details/6792972 Automatic Reference ...
- [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode
转载地址:http://choijing.iteye.com/blog/1860761 后来发现是编译选项的问题: 1.点击工程名 打开编译选项 2.在编译选项中,选择Bulid Settin ...
- Welcome-to-Swift-16自动引用计数(Automatic Reference Counting)
Swift使用自动引用计数(ARC)来跟踪并管理应用使用的内存.大部分情况下,这意味着在Swift语言中,内存管理"仍然工作",不需要自己去考虑内存管理的事情.当实例不再被使用时, ...
随机推荐
- MyEclipse 10安装SVN插件subclipse
1. 下载SVN插件subclipse 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?expandFolder=2240& ...
- 让ul li水平居中(任意删除li也能水平居中)
HTML代码: <div class="box"> <ul class="button-ct"> <li></li&g ...
- 《带你装B,带你飞》pytest修仙之路5 - yield操作
1. 简介 上一篇中,我们刚刚实现了在每个用例之前执行初始化操作,那么用例执行完之后如需要清除数据(或还原)操作,可以使用 yield 来实现.fixture通过scope参数控制setup级别,既然 ...
- B - Fadi and LCM CodeForces - 1285C 质因子
题目大意很简单,给你一个整数X,让你求a和b,使得max(a,b)尽可能的小,然后打印a,b 题解:想到了质因子分解,也考虑到了暴力,但是觉得暴力的话会TLE,所以打算用贪心做,然后就一直Wa.... ...
- ppt和pptx转图片完整代码,解决2003版和2007版中文乱码问题
引入所需依赖,注意poi版本,新版本不支持,最好使用和我一样的版本. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --& ...
- ADO.Net和Entity Framework的区别联系
它们有以下几点区别:1,ADO.Net是开发人员自己select.update等写sql语句,来实现对数据库的增删改查等操作:采用EF进行开发操作数据库的时候,只需要操作对象,这样做使开发更方便,此时 ...
- phpspider框架的使用
手册:https://doc.phpspider.org/configs-members.html 参考:https://www.jianshu.com/p/01052508ea7c 不多说,代码贴上 ...
- Golang快速入门:从菜鸟变大佬
最近写了不少Go代码,但是写着写着,还是容易忘,尤其是再写点Python代码后.所以找了一篇不错的Golang基础教程,翻译一下,时常看看. 原文链接: 「Learning Go - from zer ...
- QMessage自动定时关闭
QMessageBox *box = new QMessageBox(QMessageBox::Information,tr("test"),tr("testtest&q ...
- opencv-1-QT_OPENCV 安装
opencv-1-QT_OPENCV 安装 qtopencvc++ 既然我们是从头开始的, 那我们就从 opencv 的安装开始吧, 主要环境为: win10 1909 - 18363.720 版本 ...