Re: ARC and autorelease pools
Re: ARC and autorelease pools
- Subject: Re: ARC and autorelease pools
- From: Jens Alfke <email@hidden>
- Date: Sun, 16 Feb 2014 13:13:33 -0800
On Feb 16, 2014, at 10:22 AM, Kevin Meaney <email@hidden> wrote:
> You're missing the question I was trying to ask. Why is autorelease needed at all?
It's needed when a method creates an object [or otherwise gets an object with a reference that needs to be released] and has to return that object, but the caller isn't aware that the reference needs to be released. The method can call -autorelease on that object, which schedules a pending release in the future, balancing the ref-counting.
You could argue that if ARC were mandatory [which it isn't, remember] autorelease wouldn't be necessary because the method above can be declared as returning a strong reference, so the caller will know to release it. Unfortunately it isn't that simple, because there can be multiple implementations of a method. For example, a class might implement a -bgColor method that returns an NSColor stored in an instance variable; so the return value doesn't need to be released. But a subclass might override -bgColor to allocate and return a new NSColor instance. Now the caller _does_ need to release it, but the caller doesn't know that because it has no idea which implementation of -bgColor actually got called.
The only way to resolve this without autorelease would be to enforce that _all_ methods that return objects have to return a retained reference for the caller to release. This would end up adding a huge number of retain/release calls, which I'm pretty sure would affect performance.
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden