我希望有人能在这件事上支持我......
我一直在开发一个应用程序,该应用程序允许最终用户录制一个小音频文件以供以后播放,并且正在测试内存泄漏。当 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 会话的屏幕截图:开发者论坛:AVAudioRecorder 内存泄漏 https://devforums.apple.com/message/182669
任何想法将不胜感激!