Reputation: 177
My Xcode app plays many different audio files. There seems to be a memory leak that eventually crashes the app. Should I be releasing the memory of the audio players? What extra code do I need to release these? I have about 400 audio files.
NSString *file1Path = [[NSBundle mainBundle] pathForResource:@"50"ofType:@"mp3"];
NSURL *file1URL = [[NSURL alloc] initFileURLWithPath:file1Path];
file1Player = [[AVAudioPlayer alloc] initWithContentsOfURL:file1URL error:nil];
[file1Player play];
could I just add this code?
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[file1Player release];
file1Player=nil;
}
Upvotes: 1
Views: 281
Reputation: 437422
You have proposed adding [file1Player release]
.
If you are using automatic reference counting, that is not only not necessary, but not permitted. If you are using manual reference counting, though, it is essential. But make sure to specify the delegate
of the AVAudioPlayer
or else that method won’t be called.
If you are using manual reference counting, I’d suggest using shift+command+B (or “Product” » “Analyze”) to perform static analysis. Especially in manual reference counting code (but even in ARC Objective-C code), this static analyzer is an extremely useful tool. There is no point in proceeding until you’ve resolved all issues identified there.
Or, as you suggest, even better, convert your manual reference counting project to automatic reference counting.
Upvotes: 1