【问题标题】:AVAudioRecorder Memory LeakAVAudioRecorder 内存泄漏
【发布时间】:2011-01-29 01:40:02
【问题描述】:

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

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

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

@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”方法正在泄漏……某事。您可以在此处查看 Instruments 会话的屏幕截图:Developer Forums: AVAudioRecorder Memory Leak

任何想法将不胜感激!

【问题讨论】:

    标签: iphone audio memory-leaks record avaudiorecorder


    【解决方案1】:

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

    泄漏的方法是:

    closeFile(AVAudioRecorder*,AudioRecorderImpl*)

    这里是在运行 iOS 4.3.x 的设备上运行的应用程序完成的分析会话的屏幕截图

    【讨论】:

      【解决方案2】:

      我什么也没看到,如果 Clang 没有在您的代码中发现泄漏,那么您的代码不太可能有问题。它看起来像框架中的泄漏。

      我不会出汗的。用户按下停止时的 16 字节泄漏不太可能导致问题。在累积的泄漏变得大到足以引起问题之前,您必须停止数千次录音。只有在快速重复(例如在大循环中)时,小泄漏才是一个问题。

      留下一个已知的泄漏是令人讨厌且美观的,但时间就是金钱,除非您知道这是一个严重的问题,否则我不会浪费在这上面。

      【讨论】:

        【解决方案3】:

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

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

        我还尝试了其他方法,例如更改录制设置(不同的质量、比特率等),但没有什么太可预测而无法得出任何科学结论。

        我知道这篇文章已经有一年了,但据我所知,它还没有修复。

        【讨论】:

        • 这绝对是一个错误。有人向 Apple 提交了一个错误 (#9995333)。希望这得到修复。在开发者论坛上可能会更好地找到一些更新的信息:devforums.apple.com/message/519551#519551
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        • 2013-01-20
        • 2011-10-31
        • 2019-08-10
        相关资源
        最近更新 更多