I have been messing around with Leaks trying to find which function is not being deallocated (I am still new to this) and could really use some experienced insight.
I have this bit of code that seems to be the culprit. Every time I press the button that calls this code, 32kb of memory is additionally allocated to memory and when the button is released that memory does not get deallocated.
What I found was that everytime that AVAudioPlayer is called to play an m4a file, the final function to parse the m4a file is MP4BoxParser::Initialize() and this in turn allocates 32kb of memory through Cached_DataSource::ReadBytes
My question is, how do I go about deallocating that after it is finished so that it doesn’t keep allocating 32kb every time the button is pressed?
Any help you could provide is greatly appreciated!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//stop playing
theAudio.stop;
// cancel any pending handleSingleTap messages
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap) object:nil];
UITouch* touch = [[event allTouches] anyObject];
NSString* filename = [g_AppsList objectAtIndex: [touch view].tag];
NSString *path = [[NSBundle mainBundle] pathForResource: filename ofType:@"m4a"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio setNumberOfLoops:-1];
[theAudio setVolume: g_Volume];
[theAudio play];
}
The trick to memory management in Cocoa is to balance out any calls to
alloc,retainorcopywith a subsequent call torelease.In this case, you are sending
allocto initialize yourtheAudiovariable, but you are never sendingrelease.Assuming that you will only have one sound playing at a time, the best way to do this is with a property on your controller (the one that has this
-touchesBeganmethod). The property declaration would look like this:You will then need to set
theAudiotonilin yourinitmethod:And be sure to release the variable in your
deallocmethod:Now, your
touchesBegancould look like this: