开发者

Xcode debug and NSArray memory management Problem

I write an app according to Aaron Hillegass'S COCOA PROGRAMMING book (Chapter 6).

The app shows available voices of Speech Synthesizer.

The init and delegate method for the table view are below:

- (id)init
{
    [super init];
    NSLog(@"init");
    speechSynth = 开发者_如何学运维[[NSSpeechSynthesizer alloc] init];
    [speechSynth setDelegate:self];
    availableVoices = [[NSSpeechSynthesizer availableVoices] retain];
    return self;
}


- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn*)aTableColumn row:(NSInteger)rowIndex
{
    NSString * aVoice = [availableVoices objectAtIndex:rowIndex];
    NSDictionary *voiceDict = [NSSpeechSynthesizer attributesForVoice:aVoice];
    return [voiceDict objectForKey:NSVoiceName];
}

I have question 1 about this line:

availableVoices = [[NSSpeechSynthesizer availableVoices] **retain**];

Why retain? I tried without retain, the window pops up, but i move the mouse on the window, the program trashed :

(gdb) continue 2011-02-13 15:57:37.671 SpeakLine[4384:80f] ** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x187e20*

Question 2:

I debug this program, even i didn't write retain, availableVoices alse can be used, but the Xcode debugger only shows nine contents, why? How can watch all the contents of the array?

This is the snapshot

Question 3:

Why the program crashed midterm not at the beginning? When were the contents of NSSpeechSynthesizer released?


Note that your init pattern is wrong.

It should be:

- (void) init
{
   self = [super init];
   if (self) {
      ... init stuff here ...
   }
   return self;
}

availableVoices = [[NSSpeechSynthesizer availableVoices] retain];

Why retain? I tried without retain, the window pops up, but i move the mouse on the window, the program trashed :

This is covered in the Objective-C Memory Management Guide; in short, if you don't new, retain, alloc, or copy an object, you must retain it if you want it to stick around.

I debug this program, even i didn't write retain, availableVoices alse can be used, but the Xcode debugger only shows nine contents, why? How can watch all the contents of the array?

Not clear what you are asking. Are there supposed to be more than 9? Is it supposed to change? Note that once an object is released, the behavior upon messaging it is undefined. It'll work sometimes until memory is overwritten.

Finally, it isn't clear what you are asking in #3. The speech synthesizer doesn't seem to be released at all.


Question 1

When you receive an object that has been autoreleased in order to take ownership of the object you need to do retain. When you are done with the object you need to correspondingly release it.

Question 2

Because you have received an object that you haven't taken ownership of results may be unpredictable.

Question 3

An autoreleased object will eventually be released by the system, since you didn't retain it will disappear at one point, typically when the nsautoreleasepool is released.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜