这些对象什么时候在ARC下发布? [英] When do these objects get released under ARC?

查看:135
本文介绍了这些对象什么时候在ARC下发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个关于ARC(自动引用计数)的问题:

I have a few questions about ARC (automatic reference counting):

CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
//Question 1: Here, I would expect the NSURL object to be autoreleased and
//therefore the CFURLRef will also be available for "a while." Is this correct?

url = NULL;
//Question 2: Will this cause the NSURL to be released immediately?







NSURL *url = [NSURL fileURLWithPath:appPath];
url = nil;
//Question 3: Does the "url = nil" result in an immediate release of the NSURL?







NSURL *url = [[NSURL alloc] initWithString:@"/something"];
url = nil;
//Question 4: What about this?


推荐答案


CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
//Question 1: Here, I would expect the NSURL object to be autoreleased and
//therefore the CFURLRef will also be available for "a while." Is this correct?


是的,因为它们是同一个对象。只有类型更改;它仍然是对同一个对象的同一引用。

Yes, because they're the same object. Only the type changes; it is still the same reference to the same object.

这说,期待一个对象继续一段时间可用是脆弱的。如果你打算继续使用那个对象,拥有它至少那么长。

That said, expecting an object to continue to "be available for ‘a while’" after you release your last ownership of it is fragile. If you intend to keep using that object, own it for at least that long. Release it when you're done with it, and no sooner.


url = NULL;
//Question 2: Will this cause the NSURL to be released immediately?

NSURL *url = [NSURL fileURLWithPath:appPath];
url = nil;
//Question 3: Does the "url = nil" result in an immediate release of the NSURL?


是和否。

对强变量赋值(除非另有说明,变量都是隐式强制的)立即释放以前的值并保留新值。每个NSURL对象的分配导致对象被保留; nil 的每个赋值导致释放之前持有的对象。

An assignment to a strong variable (and variables are implicitly strong unless otherwise specified) releases the previous value immediately and retains the new value. Each assignment of an NSURL object caused the object to be retained; each assignment of nil caused the previously-held object to be released.

但是,在这两种情况下,对象可能已由 fileURLWithPath:自动释放。这个自动释放,一如既往,不会早于自动释放池的结束,所以对象可以继续生存直到那时。这通常不是一个问题,但它偶尔会弹出,如果你做了很多对象(例如,在一个紧的循环)。

However, in both cases, the URL object may have been autoreleased by fileURLWithPath:. That autorelease, as always, comes due no sooner than the end of the autorelease pool, so the object may continue to live until then. That generally isn't a problem, but it can occasionally pop up if you're making a lot of objects (e.g., in a tight loop).

至于你担心,是的, nil 的每个赋值都会释放之前存在的对象,完成并终止对该所有权的责任。

As far as you're concerned, yes, each assignment of nil releases the object that resided there before, fulfilling and ending your responsibility to that ownership.


NSURL *url = [[NSURL alloc] initWithString:@"/something"];
url = nil;
//Question 4: What about this?


因为在这一个没有autorelease(你 alloc ked it自己),对象会立即死亡。

Same here: Your assignment releases the object. And since there's no autorelease in this one (you allocked it yourself), the object will die immediately.

这篇关于这些对象什么时候在ARC下发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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