I'm developing an iPhone 3.1.3 app.
I have the following code in viewDidLoad
method:
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
if (recorder == nil)
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03
target: self
selector: @selector(levelTimerCallback:)
userInfo: nil
repeats: YES];
Note: I check if recorder
has been created before create a new one with this code in viewDidLoad
:
if (recorder == nil)
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
When I move from this ViewController to another ViewController I do this:
[levelTimer invalidate];
levelTimer = nil;
[recorder pause];
And finally, I release recorder
on dealloc
:
- (void)dealloc {
if (recorder != nil) {
[recorder release];
}
}
Running it with Xcode option 'Run with Performance Tool', looking for Leaks, I开发者_Go百科'm getting leaks with AVAudioRecorder
.
The program will go from this first ViewController to a second ViewController. And then, when user wants, it will go from second ViewController to first ViewController and it will run first code I've added.
Is there a memory problem? Do you know a better way to do that? I want avoid been recording while user is in second viewController.
Where are you releasing recorder
. If you're releasing it in the dealloc
method, or not releasing it at all, you may have a leak, assuming the code you've pasted above is triggered in viewDidLoad
or similar. recorder
will get a new AVAudioRecorder
assigned to it every time the view reloads, but will only be released once (when the view controller is dealloc'd).
精彩评论