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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to