Dealloc warranted? Unreleased retain?


#1

By the end of Chapter 6, the init method looks something like this:

- (id)init { if(![super init]) return nil; speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice: nil]; [speechSynth setDelegate: self]; voiceList = [[NSSpeechSynthesizer availableVoices] retain]; return self; }
We created an instance of NSSpeechSynthesizer, but never released it. We retained an instance NSArray of that came from sending the availableVoices message to NSSpeechSynthesizer. Why don’t we release these guys after we’re all done with them?

That is, why don’t we have a dealloc method as below?

- (void)dealloc { [speechSynth release]; [voiceList release]; [super dealloc]; }


#2

[quote=“Glitch”]By the end of Chapter 6, the init method looks something like this:

- (id)init { if(![super init]) return nil; speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice: nil]; [speechSynth setDelegate: self]; voiceList = [[NSSpeechSynthesizer availableVoices] retain]; return self; }
We created an instance of NSSpeechSynthesizer, but never released it. We retained an instance NSArray of that came from sending the availableVoices message to NSSpeechSynthesizer. Why don’t we release these guys after we’re all done with them?

That is, why don’t we have a dealloc method as below?

- (void)dealloc { [speechSynth release]; [voiceList release]; [super dealloc]; }[/quote]

I guess Aaron omitted the dealloc definition because it’ll never get called in this app, let me quote NSObject’s dealloc documentation:

So, when you add code like

- (void)dealloc
{
	NSLog(@"dealloc");
	[speechSynth release];
	[voiceList release];
	[super dealloc];
}

you won’t see the “dealloc” message when you quit the application

But anyway, who knows if that code doesn’t end up in another project sometimes so it shouldn’t hurt to define it nevertheless IMHO

EDIT: typo


#3

[quote=“whoami”]I guess Aaron omitted the dealloc definition because it’ll never get called in this app, let me quote NSObject’s dealloc documentation: [quote]
Important: Note that when an application terminates, objects may not be sent a dealloc message since the process’s memory is automatically cleared on exit — it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods.
[/quote] […snip…]
But anyway, who knows if that code doesn’t end up in another project sometimes so it shouldn’t hurt to define it nevertheless IMHO[/quote]
Ah, that makes sense. For the next printing and/or edition, I think it would be a service to the reader to mention this point. The point that, (1) in a practical sense, the dealloc isn’t needed in the context of the exercise presented because of what’s stated above, but (2) that in situations where the instance may be deallocated before the application is terminated, the dealloc would be required for proper memory management.

Mentioning this in the text would help reinforce the practice of retain-release memory management and good habits in the mind of the reader.

PS - Thanks, whoami. :slight_smile: