【发布时间】:2010-08-08 22:08:13
【问题描述】:
我正在尝试了解 Objective-C 中的内存管理。到目前为止,我一直在使用垃圾收集器,但在继续之前,我想更好地了解手动管理内存。我知道我在这段代码中没有实现 dealloc 方法。
我的问题是为什么我的 inputString 变量在这里的保留计数为 11?
#import "AppController.h"
@implementation AppController
-(id) init
{
[super init];
NSLog(@"init");
speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
NSLog(@"speechSynth retain count is %d",[speechSynth retainCount]);
return self;
}
-(IBAction) count:(id) sender
{
NSString *outputString;
int numberOfCharacters;
inputString = [textField stringValue];
numberOfCharacters = [inputString length];
outputString = [NSString stringWithFormat:@"\"%@\" has %d characters",inputString,numberOfCharacters];
[label setStringValue:outputString];
[speechSynth startSpeakingString:outputString];
NSLog(@"outputString retain count is : %i",[outputString retainCount]);
NSLog(@"inputString retain count is: %d",[inputString retainCount]);
NSLog(@"speechSynth retain count is: %d",[speechSynth retainCount]);
[outputString release];
}
@end
【问题讨论】:
标签: objective-c memory-management