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 NSAutoreleasePools anymore, however there is a new syntax:
在ARC中,我们不再需要手动管理内存.ARC已经不允许你创建NSAutoreleasePools了,然而有一种新的语法格式:
@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 ...
随机推荐
- Appium+python 一个简单的登录测试实例
# coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTestRun ...
- JQuery - 特殊字符转义(Uncaught Error: Syntax error, unrecognized expression:的处理)
今天在改一个jQuery老项目时候,发现标签上的data-id中含有特殊字符时候报错Uncaught Error: Syntax error, unrecognized expression,如何处理 ...
- LoadRunner中自定义C函数实现字符串替换
.在globals.h 中定义一个函数ReplaceStr,实现字符串的替换: int ReplaceStr(char* sSrc, char* sMatchStr, char* sReplaceSt ...
- grep 同时排除多个关键字
不说废话, 例如需要排除 abc.txt 中的 mmm nnn grep -v 'mmm\|nnn' abc.txt 再举个例子,需要确定mac 的本机ip地址, 显然直接可以输入 ifcon ...
- Webpack, VSCode 和 Babel 组件模块导入别名
很多时候我们使用别人的库,都是通过 npm install,再简单的引入,就可以使用了. 1 2 import React from 'react' import { connect } fr ...
- 转:使用python的Flask实现一个RESTful API服务器端
提示:可以学习一下flask框架中对于密码进行校验的部分.封装了太多操作. 最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了 ...
- 洛谷P1088 火星人 [STL]
题目传送门 火星人 格式难调,题面就不放了. 分析: 这道题目不得不又让人感叹,还是$STL$大法好!!! $C++$的$algorithm$库中自带有$next\_permutation()$和$p ...
- POJ3255 Roadblocks [Dijkstra,次短路]
题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...
- OpenGL的一些重要函数记录
glViewport - set the viewport https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glViewport.x ...
- 类命名空间与对象、实例的命名空间 and 面向对象的组合用法
类命名空间与对象.实例的命名空间 创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字称为类的属性 而类有两种属性:静态属性和动态属性 静态属性就是直接在类中定义的变量 动态属性就 ...