gpt4 book ai didi

iphone - AVAudioRecorder 内存泄漏

转载 作者:行者123 更新时间:2023-12-03 20:36:50 25 4
gpt4 key购买 nike

我希望有人能支持我......

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

为了说明这一点,我开发了一个精简的测试应用程序,它除了通过按下按钮来开始/停止录制之外什么也不做。为了简单起见,一切都发生在应用程序中。委托(delegate)如下:

@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”方法正在泄漏......某些东西。您可以在此处查看仪器 session 的屏幕截图:Developer Forums: AVAudioRecorder Memory Leak

如有任何想法,我们将不胜感激!

最佳答案

我没有看到任何东西,如果 Clang 没有在你的代码中发现泄漏,你的代码不太可能有错误。看起来像是框架中存在泄漏。

我不会担心。用户按下停止键时发生 16 字节泄漏不太可能导致问题。在累积的泄漏量大到足以引起问题之前,您必须停止数千次录音。小泄漏仅在快速重复(例如在大循环中)时才需要考虑。

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

关于iphone - AVAudioRecorder 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2470744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com