• 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: Autorelease question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Autorelease question


  • Subject: Re: Autorelease question
  • From: Adam Leonard <email@hidden>
  • Date: Thu, 3 Apr 2008 17:50:25 -0700

Hi,
This is all explained for you here: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concepts/AutoreleasePools.html
and here: http://developer.apple.com/documentation/Cocoa/Conceptual/CarbonCocoaDoc/Articles/WrapperFunctions.html#/ /apple_ref/doc/uid/20002398-TP1


To summarize:
If you are writing a tool that links with Foundation and not AppKit, an autorelease pool is NOT created for you. As you saw in your first example, you need to explicitly create and destroy one whenever you are dealing with Foundation objects in a method.


If you are writing an application that links with AppKit (and Foundation), autorelease pools are managed for you, and one should always exist when calling any obj-c method. Usually, they are created before dealing with an event, and destroyed after everything returns to main. (This all assumes you keep the main function generated by the xcode template intact)

If you are writing an application in Carbon that uses AppKit, it depends. I assume you are doing this in your second example, and calling NSApplicationLoad somewhere. Usually, it is better just to always explicitly create an autorelease pool, but it is created for you in some contexts, as you are seeing.
I don't think there is any public API for checking if an autorelease pool is in place before, but if you are working with a complex carbon situation, it never hurts to just create your own to be sure.



Also, a small note:

Aaron Hillehass in his book, said that if object wasn't created with
alloc, new, copy or mutableCopy then that object will be added
to the default autorelease pool

Reread that sentence in Hillegass's book.It says to "assume...[the object] is in the autorelease pool". It will not always be the case that an object not created with alloc, copy, etc. will be in an autorelease pool. It might be a singleton or cached in some way. These are usually implementation details, but just watch out.



Adam Leonard _______________________________________________

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: Autorelease question
      • From: Jens Alfke <email@hidden>
References: 
 >Autorelease question (From: Dmitry Markman <email@hidden>)

  • Prev by Date: NSCondition easter-egg hunt
  • Next by Date: Re: Autorelease question
  • Previous by thread: Re: Autorelease question
  • Next by thread: Re: Autorelease question
  • Index(es):
    • Date
    • Thread