ARC中的@autoreleasepool还有作用吗?
ARC中的@autoreleasepool还有作用吗?
QUESTION
For the most part with ARC (Automatic Reference Counting), we don't need to think about memory management at all with Objective-C objects. It is not permitted to create NSAutoreleasePool
s anymore, however there is a new syntax:
在ARC中,我们不再需要手动管理内存.ARC已经不允许你创建NSAutoreleasePool
s了,然而有一种新的语法格式:
@autoreleasepool{…}
My question is, why would I ever need this when I'm not supposed to be manually releasing/autoreleasing ?
我的问题是,为什么@autoreleasepool
可以在ARC中使用而不出错呢?
ANSWER
There seems to be a lot of confusion on this topic (and at least 80 people who probably are now confused about this and think they need to sprinkle @autoreleasepool around their code).
这个主题看起来相当迷惑人(至少80人搞不清楚状况,或许他们会将@autoreleasepool从他们的代码中移除掉)
If a project (including its dependencies) exclusively uses ARC, then @autoreleasepool never needs to be used and will do nothing useful. ARC will handle releasing objects at the correct time. For example:
如果一个工程完全由ARC编译(包括它依赖的工程项目),那么@autoreleasepool 完全没有用处,ARC会自己管理好对象的内存释放问题,例如:
@interfaceTesting:NSObject+(void) test;
@end
@implementation Testing
-(void) dealloc
{
NSLog(@"dealloc");
}
+(void) test
{
while(true)
NSLog(@"p = %p",[Testing new]);
}
@end
displays:
p =0x17696f80
dealloc
p =0x17570a90
dealloc
Each Testing object is deallocated as soon as the value goes out of scope, without waiting for an autorelease pool to be exited. (The same thing happens with the NSNumber example; this just lets us observe the dealloc.) ARC does not use autorelease.
每一个Testing对象,在出了值的作用域之后都会被立马释放掉了,完全不需要有一个 autorelease 存在,ARC中使用autorelease完全没有用处.
The reason @autoreleasepool is still allowed is for mixed ARC and non-ARC projects, which haven't yet completely transitioned to ARC.
你之所以可以继续使用@autoreleasepool 的原因是因为如果你的工程是ARC与MRC混合的,工程中并没有完全的转换成ARC而已.
If you call into non-ARC code, it may return an autoreleased object. In that case, the above loop would leak, since the current autorelease pool will never be exited. That's where you'd want to put an @autoreleasepool around the code block.
如果你在MRC代码中调用了,它会给你返回一个autoreleased 的对象.在那种情形中,上面的循环就会泄露,所以,为了兼容MRC,你需要给你的相关代码添加@autoreleasepool.
But if you've completely made the ARC transition, then forget about autoreleasepool.
如果你的项目完全由ARC编译的,那么请忘掉这个autoreleasepool :).
ARC中的@autoreleasepool还有作用吗?的更多相关文章
- @autoreleasepool在MRC和ARC中的区别
对于@autoreleasepool {} (1)在ARC中会销毁所有在里面创建的对象,即使你用外面的Strong指针指向他 (2)在MRC中如果有外部的强指针指向,不会销毁对象,retainCoun ...
- 如何实现ARC中weak功能?
原文链接 我们都知道ARC中weak与assign或者说unsafe_unretained最大的不同就是设置weak属性后,系统会在对象被释放后自动将指向对象的指针置为nil,而assign则会产生一 ...
- block使用小结、在arc中使用block、如何防止循环引用
引言 使用block已经有一段时间了,感觉自己了解的还行,但是几天前看到CocoaChina上一个关于block的小测试主题: [小测试]你真的知道blocks在Objective-C中是怎么工作的吗 ...
- SQLSERVER中NULL位图的作用
SQLSERVER中NULL位图的作用 首先感谢宋沄剑提供的文章和sqlskill网站:www.sqlskills.com,看下面文章之前请先看一下下面两篇文章 SQL Server误区30日谈-Da ...
- PHP中的header()函数作用
PHP 中 header()函数的作用是给客户端发送头信息. 什么是头信息?这里只作简单解释,详细的自己看http协议.在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(h ...
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- ARC中KVO开发注意
1 在ARC 中 KVO开发 添加监听和去掉监听必需 一一匹配,不要有过的去掉监听否则会有可能导致对象无法释放. 例如,在一个viewcontroller中添加webview 并监听webview的c ...
- log4net日志在app.config中assembly不起作用
log4net 1.2.15.0日志在app.config中assembly不起作用,必须 1.手动调用方法log4net.Config.XmlConfigurator.Configure()来初始化 ...
- URL中“#” “?” &“”号的作用
URL中"#" "?" &""号的作用 阅读目录 1. # 2. ? 3. & 回到顶部 1. # 10年9月,twit ...
随机推荐
- 用 python 来操作 docx, xlsx 格式文件(二)(使用 docx 库操作 docx 格式文件
docx 库 文章结构: 一.docx 基本用,创建 docx 文件并添加数据 二.深入理解文本格式(format),并设置所格式属性(attribute) 三.深入理解样式(styles),以及如何 ...
- Windows实现内网IPMI端口转发
https://www.cnblogs.com/yunweis/p/8024346.html
- Python库导入错误:ImportError: No module named matplotlib.pyplot
在Python中导入matplotlib.pyplot时出现如下错误: 在Windows操作系统下解决办法为: 打开命令提示符(按快捷键Win+r ,输入“cmd",回车),输入以下指令即可 ...
- Wannafly挑战赛18 B - 随机数
思路:化简公式,Pn 表示 进行n 次操作,有奇数次1的概率 Pn = (1 - x) * Pn - 1 + x * (1 - Pn - 1) 得通项公式 Pn = (1 - (1 - 2 * x) ...
- python的多线程threading
多线程threading 1.Thread创建线程: 上代码: #!/usr/bin/env python3 import threading import time def A(): t_name ...
- Xpath,XQuery,DTD
一.Xpath XPath 是一门在 XML 文档中查找信息的语言;XPath 是 XSLT 中的主要元素.XPath是W3C标准.1.七种类型节点:元素.属性.文本.命名空间.处理指令.注释.文档节 ...
- Socket 异步
摘要: System.Net.Sockets.Sockte 类有一组增强功能,提供可供专用的高性能套接字应用程序使用的可选异步模式,SocketAsyncEventArgs 类就是这一组增强功能的一部 ...
- 转:LNMP虚拟主机PHP沙盒绕过/命令执行(php exec命令被禁之后)
LNMP虚拟主机PHP沙盒绕过/命令执行 lnmp更新1.2版本,很多东西都升级了,很棒.不过还是发现一个BUG. LNMP是一款linux下nginx.php.mysql一键安装包. 下载:http ...
- PHP获取以为数组中的最大值和最小值
1.PHP获取一维数组中的最大值 <?php $a=array('1','3','55','99'); $pos = array_search(max($a), $a); echo $a[$po ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...