Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xcodeNewbie

macrumors member
Original poster
Jul 1, 2011
65
0
I've been having trouble getting the following bit of code to work right:
Code:
for (Planet *aPlanet in planetArray) {
        [aPlanet.makeShipTimer invalidate];
}
It always crashes when it runs this part. So I tried it like this:
Code:
for (Planet *aPlanet in planetArray) {
        if ([aPlanet.makeShipTimer isValid]) [aPlanet.makeShipTimer invalidate];
}
And it still crashes. So I added an NSLog:
Code:
for (Planet *aPlanet in planetArray) {
        if ([aPlanet.makeShipTimer isValid]) NSLog(@"Valid!");
}
And the NSLog shows up, which means that the timer is valid. Any idea what's going on?
 
My first thought is that the Planet objects have been deallocated. Run your program under Instruments using the zombies template.

isValid returning true doesn't contradict my theory. The behaviour of sending a message to a deallocated object is undefined.

isValid doesn't check if the timer is still allocated, it only checks if the timer is still capable of firing. This could be simply returning a BOOL ivar. That could be why your program doesn't crash at this point.
 
Dealloced Planets

Thanks for your reply, but I already ran an NSLog test that confirmed the planets still exist.
 
I've been having trouble getting the following bit of code to work right:
Code:
for (Planet *aPlanet in planetArray) {
        [aPlanet.makeShipTimer invalidate];
}
It always crashes when it runs this part. So I tried it like this:

A couple of thoughts:
When you say it crashes, what do you mean? Do you get a stack backtrace in the console output? Care to share?

Have you run it under the debugger, with a breakpoint on both the line within the for loop? You can re-write that code slightly to make debugging a little easier, something like:
Code:
for (Planet *aPlanet in planetArray) {
        NSTimer *makeShipTimer = [aPlanet.makeShipTimer]; // guessing on type
        [makeShipTimer invalidate];
}

You can set the breakpoint on the invalidate statement and see the result of the makeShipTimer call.
 
It also depends on what properties that the op declared in the header for the nstimer as well as if he is setting to nil anywhere after he invalidates the timer.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.