I have this method in an exterior class that gets called whenever the charachter in my game hits a wall (about once every 5 seconds on average). I dont understand it. I thaught I was on top of the memory management. Everytime the method is called, a small amount of memory is leaked (Malloc 38 or 42 bytes) This keeps happening, and the game freezes up. Here is my code:
-(void)playBoing {
int x = (arc4random()%3)+1;
NSString *path = [NSString stringWithFormat:@"/boing_0%i.aif", x];
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:path];
if (boing != nil) {
boing = nil;
boing.delegate = nil;
[boing release];
}
boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
boing.delegate = self;
boing.volume = 1;
[boing play];
}
Of course, it’s lead to memory leak
First you said, that boing is nil (but memory is not deallocated, leaked), then trying to send release message to nil. You should do it like this:
No need to check boing for nil before releasing, because sending message to nil do nothing