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语言中,内存管理"仍然工作",不需要自己去考虑内存管理的事情.当实例不再被使用时, ...
随机推荐
- Pytest系列(18)- 超美测试报告插件之allure-pytest的基础使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 官方介绍 Allure Frame ...
- lr 遇到的问题
1.Abnormal termination, caused by mdrv process termination 解决方法:修改LR中的D:\Program Files\Mercury\LoadR ...
- JDK安装详细步骤
JDK的下载与安装 在java实际编程的道路上,迈出的第一步必定是JDK的安装,因为JDK是一切java的基础,这里给出在Windows10 x64版本下的JDK1.8的详细安装步骤,其他的Windo ...
- mvc传递json数据到view简单实例
基于extjs4.2 controller //存储数据的类 public class DataLink { public string Name { get; set; } public strin ...
- Extjs简单的form+grid组合
采用的是Extjs4.2版本 http://localhost:49999/GridPanel/Index 该链接是本地连接,只是方便自己访问,读者无法正常访问. <script src=&qu ...
- C. 无穷的小数
单点时限: 1.0 sec 内存限制: 512 MB 在十进制下,我们能够很轻易地判断一个小数的位数是有穷的或无穷的,但是把这个小数用二进制表示出的情况下其有穷性和无穷性就会发生改变,比如 十进制下的 ...
- 【题解】P2480 [SDOI2010]古代猪文 - 卢卡斯定理 - 中国剩余定理
P2480 [SDOI2010]古代猪文 声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 题目描述 猪王国的文明源远流长,博大精 ...
- 【翻译】TensorFlow卷积神经网络识别CIFAR 10Convolutional Neural Network (CNN)| CIFAR 10 TensorFlow
原网址:https://data-flair.training/blogs/cnn-tensorflow-cifar-10/ by DataFlair Team · Published May 21, ...
- Ubuntu 常用环境配置记录
引言 经常使用 Ubuntu 虚拟机,双系统,WSL,服务器等等,每次配置常用开发环境都要去百度细节,故在此记录一下. 更换软件源 阿里云镜像 清华镜像 # 更新 sudo apt update &a ...
- web--ajax--json
案例: 其中,用eval()解析的话,如果json有一些代码,如:{"age":"alert(你好)"},他也会帮你实行,而JOSN.parse();则会自动报 ...