ARC和自动释放 [英] ARC and autorelease

查看:117
本文介绍了ARC和自动释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

autorelease用于返回的函数对象,因此调用者不会获取所有权,callee将来会释放该对象。

autorelease is used for returned function object so the caller don't take ownership and callee will release the object in the future.

但是,ARC能够计算调用者的所有权并在使用后释放它,也就是说,它可以像C ++中的智能指针一样行为。使用ARC,它可以摆脱自动释放,因为自动释放是非确定性的。

However, ARC is capable to count ownership of caller and release it after use, that is, it can behavior just like Smart Pointer in C++. With ARC, it can get rid of autorelease because autorelease is non-deterministic.

我要求这个问题的原因是我确实看到返回的对象在前面调用了dealloc ARC比非ARC代码。这让我认为ARC可以像Smart Pointer一样,并且可以使autorelease无用。这是真的还是可能的?我唯一可以考虑自动释放有用的是多线程或网络代码,因为在对象传递时计算所有权可能并不容易。

The reason I ask for this question is that I do see the returned object calls dealloc earlier in ARC than non-ARC code. This leads me to think that ARC can behvior like Smart Pointer and can make autorelease useless. Is it true or possible? The only thing I can think about autorelease usefullness is in multip-thread or network code because it may be not easier to count the ownership when the object is passing around.

谢谢为了你的想法。

这是新的编辑,让事情变得清晰:

Here is new edit to make thing clear:

with autorelease

with autorelease

+ (MyClass*) myClass
{
    return [[[MyCClass alloc] init] autorelease];
}

- doSomething
{
   MyClass *obj = [MyClass myClass];
}

使用ARC:

+ (MyClass*) myClass
{
    return [[MyCClass alloc] init]; // no autorelease
}

- doSomething
{
   MyClass *obj = [MyClass myClass];
   // insert [obj release]
}

所以,我们真的不喜欢不需要自动释放。

So, we really don't need autorelease.

推荐答案

作为一种机制的自动释放仍由ARC 使用,而且还编译了ARC -code旨在与MRC编译代码无缝互操作,因此自动释放机制就在附近。

Autorelease as a mechanism is still used by ARC, furthermore ARC compiled-code is designed to interoperate seamlessly with MRC compiled-code so the autorelease machinery is around.

首先,不要考虑参考计数,而是考虑所有权兴趣 - 只要对象中有声明的所有者权益,那么对象就存在,当没有所有者权益时,它就会被销毁。在MRC中,您通过使用 retain 或通过创建新对象来声明所有者权益;并且您通过使用 release 放弃所有者权益。

First, don't think in terms of reference counts but in terms of ownership interest - as long as there is a declared ownership interest in an object then the object lives, when there is no ownership interest it is destroyed. In MRC you declare ownership interest by using retain, or by creating a new object; and you relinquish ownership interest by using release.

现在当callee方法创建一个对象并希望返回它时对于调用者来说,被调用者正在离开所以它需要放弃所有者权益,因此调用者需要声明其所有者权益或者该对象可能被销毁。但是有一个问题,被调用者在调用者收到对象之前就完成了 - 所以当调用者放弃其所有者权益时,对象可能会在调用者有机会宣布其兴趣之前被销毁 - 不好。

Now when a callee method creates an object and wishes to return it to its caller the callee is going away so it needs to relinquish ownership interest, and so the caller needs to declare its ownership interest or the object may be destroyed. But there is a problem, the callee finishes before the caller receives the object - so when the caller relinquishes its ownership interest the object may be destroyed before the caller has a chance to declare its interest - not good.

两种解决方案用于解决这个问题:

Two solutions are used to address this:

1)该方法被声明为转移所有者权益从被调用者返回值到调用者 - 这是用于 init copy 等方法的模型。被叫方从未通知它放弃其所有者权益,并且被叫方从未声明所有者权益 - 通过协议,来电者只需接管所有权权益并随后放弃它的责任。

1) The method is declared to transfer ownership interest in its return value from the callee to the caller - this is the model used for init, copy, etc. methods. The callee never notifies it is relinquishing its ownership interest, and the callee never declares ownership interest - by agreement the caller just takes over the ownership interest and the responsibility of relinquishing it later.

2)声明该方法返回一个值,其中调用者没有所有者权益,但其他人将在短时间内保持所有者权益 - 通常直到当前运行循环周期结束。 如果调用者想要使用更长的返回值,那么必须声明自己的所有者权益,否则它可能依赖于拥有所有权利益的其他人,因此该对象就会存在。

2) The method is declared to return a value in which the caller has no ownership interest, but which someone else will maintain an ownership interest in for some short period of time - usually until the end of the current run loop cycle. If the caller wants to use the return value longer than that is must declare its own ownership interest, but otherwise it can rely on someone else having an ownership interest and hence the object staying around.

问题是谁能够某人维护所有权利益?它不可能是被调用者方法,因为它即将消失。输入自动释放池 - 这只是一个任何人都可以转移所有权利益的对象,因此该对象将保持一段时间。当被指示时,自动释放池将以这种方式放弃对所有传输给它的对象的所有权利益 - 通常在当前运行循环周期结束时。

The question is who can that "someone" be who maintains the ownership interest? It cannot be the callee method as it is about to go away. Enter the "autorelease pool" - this is just an object to which anybody can transfer an ownership interest to so the object will stay around for a while. The autorelease pool will relinquish its ownership interest in all the objects transferred to it in this way when instructed to do so - usually at the end of the current run loop cycle.

现在,如果上述内容有意义(即如果我清楚地解释了),你可以看到方法(2)并不是真的需要,因为你总是可以使用方法(1); 但是,它是一个至关重要的但是,在MRC下对程序员来说是一项更多的工作 - 从方法中获得的每个价值都带有必须管理的所有者权益,在某些时候放弃 - 生成一个字符串只是为了输出它?那么你需要放弃你对那个临时字符串的兴趣...所以(2)让生活变得更容易。

Now if the above makes any sense (i.e. if I explained it clearly), you can see that method (2) is not really required as you could always use method (1); but, and its a crucial but, under MRC that is a lot more work for the programmer - every value received from a method comes with an ownership interest which must be managed and relinquished at some point - generate a string just to output it? Well you then need to relinquish your interest in that temporary string... So (2) makes life a lot easier.

另一方面,计算机只是快速的白痴,并且计算事物和插入代码以代表智能程序员放弃所有权利益是他们非常适合的。因此ARC不需要自动发布池。但它可以使事情变得更容易和更有效,并且在幕后ARC优化其使用 - 查看Xcode中的汇编程序输出,您将看到名称类似于retainAutoreleasedReturnValue的例程调用......

One the other hand computers are just fast idiots, and counting things and inserting code to relinquish ownership interest on behalf of the intelligent programmers is something they are well suited to. So ARC doesn't need the auto release pool. But it can make things easier and more efficient, and behind the scenes ARC optimises its use - look at the assembler output in Xcode and you'll see calls to routines with name similar to "retainAutoreleasedReturnValue"...

所以你是对的,它不是需要,但它仍然有用 - 但在ARC下你可以(通常)忘记它甚至存在。

So you are right, its not needed, however it is still useful - but under ARC you can (usually) forget it even exists.

HTH比它可能混淆的更多!

HTH more than it probably confuses!

这篇关于ARC和自动释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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