我正在开发一个iPhone 3.1.3应用程序。
我在viewDidLoad方法中有以下代码:
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
if (recorder == nil)
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03
target: self
selector: @selector(levelTimerCallback:)
userInfo: nil
repeats: YES];注意:在viewDidLoad中用下面的代码创建一个新的recorder之前,我会检查是否已经创建了它
if (recorder == nil)
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];当我从这个ViewController移到另一个ViewController时,我这样做:
[levelTimer invalidate];
levelTimer = nil;
[recorder pause];最后,我在dealloc上发布了recorder
- (void)dealloc {
if (recorder != nil) {
[recorder release];
}
}使用Xcode选项'Run with Performance Tool‘运行它,寻找漏洞,我发现AVAudioRecorder有漏洞。
程序将从第一个ViewController转到第二个ViewController。然后,当用户需要时,它将从第二个ViewController转到第一个ViewController,并运行我添加的第一个代码。
有没有记忆问题?你知道更好的方法吗?我想避免被记录,而用户是在第二个viewController。
发布于 2011-07-05 16:45:28
你要在哪里发布recorder。如果你在dealloc方法中释放它,或者根本不释放它,假设上面粘贴的代码是在viewDidLoad或类似的方法中触发的,那么你可能会有泄漏。每次视图重新加载时,recorder都会获得一个分配给它的新AVAudioRecorder,但只会释放一次(当视图控制器被解除分配时)。
https://stackoverflow.com/questions/6579948
复制相似问题