gpt4 book ai didi

objective-c - Xcode调试和NSArray内存管理问题

转载 作者:行者123 更新时间:2023-12-03 16:33:39 24 4
gpt4 key购买 nike

我根据 Aaron Hillegass 的《COCOA PROGRAMMING》一书(第 6 章)编写了一个应用程序。

该应用程序显示语音合成器的可用声音。

TableView 的初始化和委托(delegate)方法如下:

- (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];
}

我有关于这一行的问题 1:

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

为什么要保留?我尝试不保留,弹出窗口,但我将鼠标移到窗口上,程序被丢弃:

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

问题2:

我调试这个程序,即使我没有写retain,availableVoices也可以使用,但是Xcode调试器只显示九个内容,为什么?如何查看数组的所有内容?

<强> This is the snapshot

问题3:

为什么程序在中期崩溃而不是在开始时崩溃?NSSpeechSynthesizer 的内容是什么时候发布的?

最佳答案

请注意,您的 init 模式是错误的。

应该是:

- (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 :

这在 Objective-C Memory Management Guide 中有介绍。 ;简而言之,如果您不新建、保留、分配或复制对象,那么如果您希望它保留下来,则必须保留它。

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?

不清楚你想问什么。应该有9个以上吧?是不是应该改变一下?请注意,一旦对象被释放,消息传递时的行为是不确定的。有时它会起作用,直到内存被覆盖为止。

最后,不清楚您在#3 中要问什么。语音合成器似乎根本没有发布。

关于objective-c - Xcode调试和NSArray内存管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com