Re: Hillegass book memory question
Re: Hillegass book memory question
- Subject: Re: Hillegass book memory question
- From: Adam Leonard <email@hidden>
- Date: Mon, 2 Jun 2008 22:10:38 -0700
Hi,
I have the second edition still, but I assume this example has not
changed much in the third edition.
Usually, yes, you must release anything you +alloc, -retain, -copy,
etc. This is a bit of a special case though.
In this case, exactly one NSSpeechSynthesizer is allocated when the
application is launched (in AppController's -init), and it is used for
almost the entire life of the application; i.e., the same speech
synthesizer is always used whenever one of the buttons is pressed
until the user quits the application.
However, the operating system will always reclaim all memory you
malloc'd when the application quits. It just takes the entire memory
space your application used and say it is "free" for other
applications to use.
So, it is very common to take advantage of this fact. The speech
synthesizer object is automatically dealloc'd by the OS very soon
after you are done using it, so there is no reason to explicitly
release it yourself.
The Cocoa frameworks take this shortcut, as do most applications.
By the way, this shortcut is not just the result of laziness, it is
actually faster. Instead of going through all the objects that are
used throughout the life span of the app, and having the OS free all
of them one at a time, the shortcut lets the OS group all these
objects and free them at the same time.
It is, however, perfectly acceptable to override -dealloc in
AppController and call [speachSynthesizer release]. I am sure this is
discussed elsewhere in the book.
Hope that makes sense.
Adam Leonard
On Jun 2, 2008, at 9:39 PM, Ashley Perrien wrote:
I'm working my way through the 3rd edition and have a question
specifically on speech synthesizer in chapter 5. It's my
understanding that whenever something is retained or alloced they
eventually need to be released. In the application built in the
chapter, NSSpeechSynthesizer is alloced and an array is retained and
neither are released. I know that for the very small and simple apps
here, it doesn't really matter but how and where would you go about
releasing those objects? If they shouldn't be released, why not?
Thanks for any enlightenment.
Ashley Perrien
_______________________________________________
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
_______________________________________________
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