Hello, I've been having trouble with my memory management concerning some timers. I have a class called "Planet" that uses two timers as instance variables. The planet objects I create aren't being deallocated when they should as their retain count is too high. Below is a line of code from my initialization method of Planet:
This comes right at the beginning of my initialization method. As I would expect, the NSLOG prints out 1. However, If I add the NSLOG after the timers are created:
The NSLOG prints 3!?!?! For some reason the timers are retaining the planet object that creates them. I'm not sure if this is normal behavior or not but it's making it so that the planets don't get deallocated when I'm through with them. Its giving me major memory leaks. Can somebody please help?
Code:
NSLog(@"%i", [self retainCount]);
self.battleTimer = [NSTimer scheduledTimerWithTimeInterval:0.3f target:self selector:@selector(battle) userInfo:nil repeats:YES];
self.makeShipTimer = [NSTimer scheduledTimerWithTimeInterval:(60-radius)/15 target:self selector:@selector(makeShip) userInfo:nil repeats:YES];
Code:
self.battleTimer = [NSTimer scheduledTimerWithTimeInterval:0.3f target:self selector:@selector(battle) userInfo:nil repeats:YES];
self.makeShipTimer = [NSTimer scheduledTimerWithTimeInterval:(60-radius)/15 target:self selector:@selector(makeShip) userInfo:nil repeats:YES];
NSLog(@"%i", [self retainCount]);
The NSLOG prints 3!?!?! For some reason the timers are retaining the planet object that creates them. I'm not sure if this is normal behavior or not but it's making it so that the planets don't get deallocated when I'm through with them. Its giving me major memory leaks. Can somebody please help?