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语言中,内存管理"仍然工作",不需要自己去考虑内存管理的事情.当实例不再被使用时, ...
随机推荐
- 【python实现卷积神经网络】损失函数的定义(均方误差损失、交叉熵损失)
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- 【python实现卷积神经网络】优化器的实现(SGD、Nesterov、Adagrad、Adadelta、RMSprop、Adam)
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- MAC 上brew 更新 出错
在MAC上brew update的时候出现报错:Error: /usr/local must be writable! 错误,在该文章中也给出解决办法(sudo chown -R $(whoami) ...
- 用网线连接Windows和Linux台式机,并实现Linux共享Windows的WiFi网络
前言 由于工作需要,需要利用网线将自己的笔记本和Linux台式机进行连接,实现Windows可以远程登录Linux机器,并实现Linux共享Windows的WiFi网络. 网上的很多方法可以实现两台机 ...
- Android | 教你如何在安卓上实现通用卡证识别,一键各种卡绑定
目录 前言 通用卡证识别的应用场景 如何使用通用卡证识别服务 集成通用卡证识别服务的关键流程 开发实战 1 开发准备 1.1 在项目级gradle里添加华为maven仓 1.2 在应用级的build. ...
- react: typescript custom hooks useAsyncTable
define basic data: const SET_QUERY = "SET_QUERY"; const TOGGLE_LOADING = "TOGGLE_LOAD ...
- Centos8安装docker-compose
一.首先检查是否有pip 执行命令:piv -V 二.更新pip 执行命令:pip install --upgrade pip 三.下载 setuptools 执行命令 :pip install - ...
- py安装教程
https://www.runoob.com/w3cnote/pycharm-windows-install.html
- 使用 Python 控制自己的电脑和键盘是一种什么样的体验?python学习的正确姿势
可能有时候你需要在电脑做一些重复的点击或者提交表单等操作,如果能通过 Python 预先写好相关的操作指令,让它帮你操作,然后你自己爱干嘛干嘛去,有点 “按键精灵” 的意思,是不是感觉有点爽呢? 那么 ...
- Spring Boot JPA中关联表的使用
文章目录 添加依赖 构建Entity 构建Repository 构建初始数据 测试 Spring Boot JPA中关联表的使用 本文中,我们会将会通过一个Book和Category的关联关系,来讲解 ...