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. Python操作rabbitmq系列(三):多个接收端消费消息

    接着上一章.这一章,我们要将同一个消息发给多个客户端.这就是发布订阅模式.直接看代码: 发送端: import pikaimport sys connection = pika.BlockingCon ...

  2. python3(三十八) serialize

    """ """ __author__on__ = 'shaozhiqi 2019/9/24' # !/usr/bin/env python3 ...

  3. python3(三十二) try except

    """ 异常处理 """ __author__on__ = 'shaozhiqi 2019/9/19' # 大量的代码来判断是否出错: # ...

  4. std::string构造函数

    string(); string (const string& str); string (const string& str, size_t pos, size_t len = np ...

  5. AtomicInteger的并发处理

    AtomicInteger的并发处理 博客分类: Effective Java   JDK1.5之后的java.util.concurrent.atomic包里,多了一批原子处理类.主要用于在高并发环 ...

  6. HashMap实现原理(JDK1.8)

    概述HashMap在底层数据结构上采用了数组+链表+红黑树,通过散列映射来存储键值对数据因为在查询上使用散列码(通过键生成一个数字作为数组下标,这个数字就是hash code)所以在查询上的访问速度比 ...

  7. sql 自增序列

    一.使用set identity_insert [database][owner][table]on设置时,要在插入语句中显示列出插入的列;

  8. 从python爬虫以及数据可视化的角度来为大家呈现“227事件”后,肖战粉丝的数据图

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取t.cn ...

  9. 当文件目录变得杂乱不堪怎么办,python帮你轻松搞定

    这几天和几个小伙伴,在一起合做一个ppt. 做ppt之前有原版的ppt,和一个word大纲,在制作过程中,又不断添加图片.视频等素材,最终,整个目录变得杂乱不堪(见下图-处理之前) 那我想,可不可以做 ...

  10. 对短路变形POJ3615

    Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are ...