为什么ARC保留方法参数? [英] Why does ARC retain method arguments?

查看:105
本文介绍了为什么ARC保留方法参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ARC编译时,方法参数通常显示为保留在方法开头,并在结束时释放。这个保留/释放对似乎是多余的,并且与ARC生成你将会已经写的代码的想法相矛盾。没有人在那些黑暗的,ARC之前的日子对所有的方法参数执行额外的保留/释放只是为了安全,他们吗?

When compiling with ARC, method arguments often appear to be retained at the beginning of the method and released at the end. This retain/release pair seems superfluous, and contradicts the idea that ARC "produces the code you would have written anyway". Nobody in those dark, pre-ARC days performed an extra retain/release on all method arguments just to be on the safe side, did they?

考虑:

@interface Test : NSObject
@end

@implementation Test

- (void)testARC:(NSString *)s
{
  [s length];  // no extra retain/release here.
}

- (void)testARC2:(NSString *)s
{
  // ARC inserts [s retain]
  [s length];
  [s length];
  // ARC inserts [s release]
}

- (void)testARC3:(__unsafe_unretained NSString *)s
{
  // no retain -- we used __unsafe_unretained
  [s length];
  [s length];
  // no release -- we used __unsafe_unretained
}

@end

当使用Xcode 4.3.2在发布模式下编译时,程序集(使我能够理解它)包含对 objc_retain code> objc_release 在第二个方法的开始和结束。发生了什么事?

When compiled with Xcode 4.3.2 in release mode, the assembly (such that I'm able to understand it) contained calls to objc_retain and objc_release at the start and end of the second method. What's going on?

这不是一个大问题,但是当使用Instruments配置性能敏感代码时,会出现额外的保留/释放流量。看起来你可以用 __ unsafe_unretained 装饰方法参数,以避免这个额外的保留/释放,就像我在第三个例子中做的,但这样做感觉非常恶心。

This is not a huge problem, but this extra retain/release traffic does show up when using Instruments to profile performance-sensitive code. It seems you can decorate method arguments with __unsafe_unretained to avoid this extra retain/release, as I've done in the third example, but doing so feels quite disgusting.

推荐答案

请参阅此回复从Objc语言邮件列表:

See this reply from the Objc-language mailing list:


当编译器不知道任何关于
内存管理行为的一个函数或方法(这发生了一个
批),那么编译器必须假设:

When the compiler doesn't know anything about the memory management behavior of a function or method (and this happens a lot), then the compiler must assume:

1)方法可能会完全重新排列或替换
应用程序的整个对象图(它可能不会,但它
可以)。 2)调用者可能是手动引用计数代码,而
因此传递参数的生命周期并不切实可行。
知道。

1) That the function or method might completely rearrange or replace the entire object graph of the application (it probably won't, but it could). 2) That the caller might be manual reference counted code, and therefore the lifetime of passed in parameters is not realistically knowable.

1和#2;并且ARC 必须从不允许对象被
过早解除分配,那么这两个假设强制编译器
保持在对象中更频繁地传递。

Given #1 and #2; and given that ARC must never allow an object to be prematurely deallocated, then these two assumptions force the compiler to retain passed in objects more often than not.

我认为主要的问题是你的方法的主体可能导致释放的参数,所以ARC必须采取防御措施并保留它们: / p>

I think that the main problem is that your method’s body might lead to the arguments being released, so that ARC has to act defensively and retain them:

- (void) processItems
{
    [self setItems:[NSArray arrayWithObject:[NSNumber numberWithInt:0]]];
    [self doSomethingSillyWith:[items lastObject]];
}

- (void) doSomethingSillyWith: (id) foo
{
    [self setItems:nil];
    NSLog(@"%@", foo); // if ARC did not retain foo, you could be in trouble
}

也是在您的方法中只有一个调用时,您看不到额外保留的原因。

That might also be the reason that you don’t see the extra retain when there’s just a single call in your method.

这篇关于为什么ARC保留方法参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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