• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: ARC and autorelease pools
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: ARC and autorelease pools
      • From: Scott Ribe <email@hidden>
References: 
 >ARC and autorelease pools (From: Kevin Meaney <email@hidden>)
 >Re: ARC and autorelease pools (From: Jens Alfke <email@hidden>)
 >Re: ARC and autorelease pools (From: Kevin Meaney <email@hidden>)

  • Prev by Date: Re: ARC and autorelease pools
  • Next by Date: Re: ARC and autorelease pools
  • Previous by thread: Re: ARC and autorelease pools
  • Next by thread: Re: ARC and autorelease pools
  • Index(es):
    • Date
    • Thread