Problem with memory leaks
Sorry, having difficulty formattin code to appear correct here???
I am trying to understand the readings I get from running instruments on my app which are telling me I am leaking memory. There are a number, quite a few in fact, that get reported from inside the Foundation, AVFoundation CoreGraphics etc that I assume I have no control over and so should ignore such as:
Malloc 32 bytes: 96 bytes, AVFoundation, prepareToRecordQueue
or Malloc 128 bytes: 128 bytes, CoreGraphics, open_handle_to_dylib_path
Am I correct in assuming these are something the system will resolve?
But then there are leaks that are reported that I believe I am responsible for, such as:
This call reports against this line leaks 2.31KB
[self createAVAudioRecorder:frameAudioFile];
Immediately followed by this:
-(NSError*) createAVAudioRecorder: (NSString *)fileName {
// flush recorder to start afresh
[audioRecorder release];
audioRecorder = nil;
// delete existing file to ensure we have clean start
[self deleteFile: fileName];
VariableStore *singleton = [VariableStore sharedInstance];
// get full path to target file to create
NSString *destinationString = [singleton.docsPath stringByAppendingPathComponent: fileName];
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];
// configure the recording settings
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:6]; //****** LEAKING 384 BYTES
[recordSettings setObject:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey]; //***** LEAKING 32 BYTES
float sampleRate = 44100.0;
[recordSettings setObject:[NSNumber numberWithFloat: sampleRate] forKey: AVSampleRateKey]; //***** LEAKING 48 BYTES
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
int bitDepth = 16;
[recordSettings setObject: [NSNumber numberWithInt:bitDepth] forKey:AVLinearPCMBitDepthKey]; //***** LEAKING 48 BYTES
[recordSettings setObject:[NSNumber numberWithBool:YES] forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setObject:[NSNumber numberWithBool: NO]forKey:AVLinearPCMIsFloatKey];
NSError *recorderSetupError = nil;
// create the new recorder with target file
audioRecorder = [[AVAudioRecorder alloc] initWithURL: destinationURL settings: recordSettings error: &recorderSetupError]; //***** LEAKING 1.31KB
[recordSettings release];
recordSettings = nil;
// check for erros
if (recorderSetupError) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Can't record" message: [recorderSetupError localizedDescription] delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
[alert show];
[alert release];
alert = nil;
return recorderSetupError;
}
[audioRecorder prepareToRecord]; //***** LEAKING 512 BYTES
audioRecorder.delegate = self;
return recorderSetupError;
}
I do not understand why there is a leak as I rel开发者_运维问答ease audioRecorder at the start and set to nil and I release recordSettings and set to nil? Can anyone enlighten me please? Thanks
Try running your app with instruments on a device. There are a few things with AVFoundation that leak on the simulator, but do not on real hardware!
but, on second thought, audioRecorder is never released. Maybe try either releasing it at the end of the method, or perhaps using autorelease?
精彩评论