这些对象何时在ARC下发布?

时间:2023-01-04 09:44:23

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

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

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?

1 个解决方案

#1


5  

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?

Yes and no.

是的,不是。

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.

赋值给强变量(除非另有说明,否则变量隐式强)会立即释放前一个值并保留新值。 NSURL对象的每个赋值都会导致保留对象;每个nil赋值都会导致先前持有的对象被释放。

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).

但是,在这两种情况下,URL对象可能已由fileURLWithPath:自动释放。自动释放,一如既往,应该在自动释放池结束之前到期,因此对象可能会继续存在直到那时。这通常不是问题,但如果您制作了大量对象(例如,在紧密的循环中),它偶尔会弹出。

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.

就你而言,是的,每个nil的任务都会释放之前存在的对象,履行并终止你对该所有权的责任。

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

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.

同样在这里:你的任务释放了对象。而且由于在这一个中没有自动释放(你自己购买),物体会立即死亡。

#1


5  

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?

Yes and no.

是的,不是。

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.

赋值给强变量(除非另有说明,否则变量隐式强)会立即释放前一个值并保留新值。 NSURL对象的每个赋值都会导致保留对象;每个nil赋值都会导致先前持有的对象被释放。

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).

但是,在这两种情况下,URL对象可能已由fileURLWithPath:自动释放。自动释放,一如既往,应该在自动释放池结束之前到期,因此对象可能会继续存在直到那时。这通常不是问题,但如果您制作了大量对象(例如,在紧密的循环中),它偶尔会弹出。

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.

就你而言,是的,每个nil的任务都会释放之前存在的对象,履行并终止你对该所有权的责任。

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

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.

同样在这里:你的任务释放了对象。而且由于在这一个中没有自动释放(你自己购买),物体会立即死亡。