AVAudioRecorder内存泄漏

时间:2010-03-18 14:54:55

标签: iphone audio memory-leaks record avaudiorecorder

我希望有人可以支持我...

我一直致力于一个应用程序,它允许最终用户录制一个小音频文件以供以后播放,并且我正在测试内存泄漏。当AVAudioRecorder的“stop”方法试图关闭它正在录制的音频文件时,我继续一直遇到内存泄漏。这似乎是框架本身的漏洞,但如果我是傻瓜,你可以告诉我。

为了说明这一点,我制作了一个精简测试应用程序,只需按一下按钮即可开始/停止录制。为了简单起见,一切都发生在应用程序中。委托如下:

@synthesize audioRecorder, button;
@synthesize window;

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
 // create compplete path to database
 NSString *tempPath = NSTemporaryDirectory();
 NSString *audioFilePath = [tempPath stringByAppendingString:@"/customStatement.caf"];

 // define audio file url
 NSURL *audioFileURL = [[NSURL alloc] initFileURLWithPath:audioFilePath];

 // define audio recorder settings
 NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
      [NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey,
      [NSNumber numberWithInt:1], AVNumberOfChannelsKey,
      [NSNumber numberWithInt:AVAudioQualityLow], AVSampleRateConverterAudioQualityKey,
      [NSNumber numberWithFloat:44100], AVSampleRateKey,
      [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
      nil
 ];

 // define audio recorder
 audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL settings:settings error:nil];
 [audioRecorder setDelegate:self];
 [audioRecorder setMeteringEnabled:YES];
 [audioRecorder prepareToRecord];

 // define record button
 button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [button addTarget:self action:@selector(handleTouch_recordButton) forControlEvents:UIControlEventTouchUpInside];
 [button setFrame:CGRectMake(110.0, 217.5, 100.0, 45.0)];
 [button setTitle:@"Record" forState:UIControlStateNormal];
 [button setTitle:@"Stop" forState:UIControlStateSelected];

 // configure the main view controller
 UIViewController *viewController = [[UIViewController alloc] init];
 [viewController.view addSubview:button];

 // add controllers to window
 [window addSubview:viewController.view];
 [window makeKeyAndVisible];

 // release
 [audioFileURL release];
 [settings release];
 [viewController release];

 return YES;
}

- (void) handleTouch_recordButton {
 if ( ![button isSelected] ) {
      [button setSelected:YES];
      [audioRecorder record];

 } else {
      [button setSelected:NO];
      [audioRecorder stop];
 }
}

- (void) dealloc {
 [audioRecorder release];
 [button release];

 [window release];

 [super dealloc];
}

来自Instruments的堆栈跟踪,清楚地显示AVFoundation代码中的“closeFile”方法正在泄漏...某事。您可以在此处查看仪器会话的屏幕截图:Developer Forums: AVAudioRecorder Memory Leak

任何想法都将不胜感激!

3 个答案:

答案 0 :(得分:3)

我没有看到任何内容,如果Clang在您的代码中没有发现泄漏,您的代码就不会出错。它看起来像是框架中的泄漏。

我不会出汗。用户按下停止时16字节的泄漏不太可能导致问题。在累积的泄漏量增加到足以导致问题之前,您将不得不停止数千次录制。小泄漏只是在快速重复时才会引起关注,例如在大循环中。

留下一个已知的泄漏是令人烦恼和美观的驱蚊,但时间就是金钱,除非你知道这是一个重大问题,否则我不会浪费。

答案 1 :(得分:2)

我很确定我可以支持你。我没有时间提交错误报告。我得到了同样的泄漏。如果我使用Apple无损格式,它通常是256个字节,但如果我使用AAC,则自身减少到48个字节。

我花了3个小时测试并试图注释掉不同的代码。没有什么是绝对可重复的,虽然我几乎每次都可以泄漏它。如果我在“停止”消息后取出一些代码,我通常可以让它不泄漏。基本上,当我在停止后立即进行一堆逻辑/运算/处理时,它会泄漏。如果我之后只是注释掉了所有代码,它就不会泄漏(通常)。

我还尝试过其他一些事情,例如改变录音设置(不同的质量,比特率等),再一次,没有什么可以预测来形成任何科学结论。

我知道这篇文章已经有一年了,但就我所见,它仍然没有修复。

答案 2 :(得分:0)

我在模拟器和设备上都遇到了同样的问题。 一旦我停止AVAudioRecorder,就会泄漏48个字节。

泄漏的方法是:

closeFile(AVAudioRecorder*,AudioRecorderImpl*)

以下是在运行iOS 4.3.x

的设备上运行的应用程序完成的分析会话的屏幕截图

enter image description here

相关问题