在自动引用计数下,为什么不允许保留,释放和取消分配? [英] Under automatic reference counting, why are retain, release, and dealloc not allowed?

查看:70
本文介绍了在自动引用计数下,为什么不允许保留,释放和取消分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 4.2中使用自动引用计数构建应用程序时尝试使用-retain-release-dealloc时,出现如下错误:

禁止自动引用计数 明确发送"dealloc"的消息

为什么我会看到此错误?自动引用计数下是否不再允许-retain-release-dealloc?

解决方案

基本上:

使用ARC时,全有或全无.编译器要么为您管理所有的retains/releases/deallocs,要么不执行任何操作.您不能散布自己的调用,因为编译器希望自己完成所有操作.通过这样做,它可以进行荒谬的优化(例如,返回手动内存管理"下的自动释放对象的方法现在可能会产生永远不会出现在自动释放池中的对象).如果您要开始使用自己的保留和释放调用,那么编译器将不得不使用这些调用,并且将无法执行它想要的(以及您应该想要的)许多优化. >

另外,调用-retainCount现在是编译器错误!哦,快乐的一天!

When trying to use -retain, -release, and -dealloc while building my application using automatic reference counting in Xcode 4.2, I get an error like the following:

Automatic Reference Counting forbids explicit message send of 'dealloc'

Why am I seeing this error? Are -retain, -release, and -dealloc no longer allowed under automatic reference counting?

解决方案

Basically:

When using ARC, it's all or nothing. Either the compiler is managing all of the retains/releases/deallocs for you, or it is doing nothing. You cannot intersperse your own calls to them, because the compiler wants to do it all itself. It can make absurd optimizations by doing this (for example, a method that returned an autoreleased object under Manual Memory Management may now produce an object that never ends up in an autorelease pool). If you were to start sprinkling in your own calls to retain and release, then the compiler would have to work with these and wouldn't be able to perform a lot of the optimizations that it wants (and that you should want).

And as an added bonus, invoking -retainCount is now a compiler error! OH HAPPY DAY!

这篇关于在自动引用计数下,为什么不允许保留,释放和取消分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆