开发者

AVAudioRecorder Memory Leak

I'm hoping someone out there can back me up on this...

I've been working on an application that allows the end user to record a small audio file for later playback an开发者_JAVA百科d am in the process of testing for memory leaks. I continue to very consistently run into a memory leak when the AVAudioRecorder's "stop" method attempts to close the audio file to which it's been recording. This really seems to be a leak in the framework itself, but if I'm being a bonehead you can tell me.

To illustrate, I've worked up a stripped down test app that does nothing but start/stop a recording w/ the press of a button. For the sake of simplicty, everything happens in app. delegate as follows:

@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];
}

The stack trace from Instruments that shows pretty clearly that the "closeFile" method in the AVFoundation code is leaking...something. You can see a screen shot of the Instruments session here: Developer Forums: AVAudioRecorder Memory Leak

Any thoughts would be greatly appreciated!


I don't see anything and if Clang doesn't find a leak in your code your code is unlikely to be at fault. It looks like a leak in the framework.

I wouldn't sweat it. A 16 byte leak when the user presses stop isn't likely to cause problem. You would have to stop thousands of recordings before the accumulated leaks grew large enough to cause problems. Small leaks are only a concern when they are rapidly repeated such as in a large loop.

It's annoying and aesthetically repellent to leave a known leak but time is money and I wouldn't waste either on this unless you know it is a significant problem.


I'm pretty sure I can back you up on this. I just haven't had time to file a bug report. I get the same leak. It usually is 256 bytes if I do Apple lossless format, but reduces itself to 48 bytes if I use AAC.

I spent 3 hours testing and trying to comment out different code. Nothing was absolutely reproducible, although I could pretty much get it to leak almost every time. If I took out some code after the "stop" message, I could usually get it to not leak. Basically, when I had a bunch of logic/crunching/processing going on immediately after doing the stop, it would leak. If I just commented out all the code after that, it wouldn't leak (usually).

I also tried other things like changing the recording settings (different qualities, bitrates, etc) and again, nothing too predictable to form any scientific conclusions.

I know this post is a year old, but it's still not fixed as far as I can see.


I'm getting the same issue, both in the Simulator and on the device. 48 bytes are being leaked, as soon as I stop the AVAudioRecorder.

The method that is leaking is:

closeFile(AVAudioRecorder*,AudioRecorderImpl*)

Here is a screenshot of a profiling session done with the App running on a device, running iOS 4.3.x

AVAudioRecorder Memory Leak

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜