I am working on a basic game, that connects to a server and gets JSON data. It works fine for a few games, but crashes soon after due to memory pressure. I ran through instruments and came across something rather disturbing. Almost every instance variable being instantiated by [[Class alloc]init] was being leaked as a NSZombie object.
As you can see in the image, in 5 seconds I seem to have generated 9000 leaks.
I am using ARC.
Further analysis showed I was leaking when used certain methods:
-(void) playTimeUp
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"Gameover"
ofType:@"wav"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
if (audioPlayer && soundShouldPlay){
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
[audioPlayer setVolume:.20];
[audioPlayer play];
[self.audioPlayers addObject:audioPlayer];
}
}
Also I use dataWithContentsOfUrl method quite often.
dispatch_async(kBackgroundQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:completeUrl];
[self performSelectorOnMainThread:@selector(startMethod:) withObject:data waitUntilDone:YES];
});
Could anyone tell me how to salvage this situation, or what I am doing wrong.
That's in the nature of zombie objects. Turning on zombie objects to debug the use of objects after they have been deallocated will obviously turn any such object into a leak. You can't debug using zombies and search for memory leaks at the same time.