我希望外面有人能支持我...
我一直在开发一个应用程序,该应用程序允许最终用户录制一个小的音频文件以供稍后回放,目前正在测试内存泄漏。当AVAudioRecorder的"stop“方法试图关闭它正在录制的音频文件时,我仍然会一直遇到内存泄漏。这看起来确实是框架本身的漏洞,但是如果我是个笨蛋,你可以告诉我。
为了说明这一点,我开发了一个精简的测试应用程序,它只需按下按钮即可开始/停止录制。为了简单起见,所有的事情都发生在app中。委派如下:
@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“方法是leaking...something。您可以在此处查看仪器会话的屏幕截图:Developer Forums: AVAudioRecorder Memory Leak
如果您有任何想法,我们将不胜感激!
发布于 2010-03-18 23:47:59
我没有看到任何东西,如果Clang没有在你的代码中发现一个漏洞,你的代码不太可能是错误的。它看起来像是框架中的一个漏洞。
我不会担心的。当用户按下stop时,一个16字节的泄漏不太可能造成问题。在累积的泄漏变得足够大以引起问题之前,您将不得不停止数千次记录。小泄漏只有在快速重复时才会引起关注,例如在大型循环中。
留下一个已知的漏洞是恼人的,也是美观的,但时间就是金钱,除非你知道这是一个重要的问题,否则我不会在这上面浪费任何时间。
发布于 2011-06-18 21:42:08
我很确定我可以在这方面支持你。我只是还没来得及提交错误报告。我也得到了同样的信息。如果我使用Apple无损格式,它通常是256字节,但如果我使用AAC,它会减少到48字节。
我花了3个小时测试并尝试注释掉不同的代码。没有什么是绝对可重现的,尽管我几乎每次都能让它泄漏。如果我在"stop“消息之后删除一些代码,我通常可以让它不会泄漏。基本上,当我在停止之后立即有一堆逻辑/处理/处理正在进行时,它会泄漏。如果我在那之后注释掉所有的代码,它通常不会泄漏。
我还尝试了其他方法,比如改变录制设置(不同的质量、比特率等),再一次,没有什么太可预测的东西,无法得出任何科学的结论。
我知道这篇文章已经有一年的历史了,但据我所知,它仍然没有被修复。
发布于 2011-08-22 19:01:23
我在模拟器和设备上都遇到了同样的问题。只要我停止AVAudioRecorder,就会泄漏48个字节。
泄漏的方法是:
closeFile(AVAudioRecorder*,AudioRecorderImpl*)
以下是在运行iOS 4.3.x的设备上运行的应用程序完成的性能分析会话的屏幕截图

https://stackoverflow.com/questions/2470744
复制相似问题