Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 14, 2011, 03:52 PM   #1
xcodeNewbie
macrumors member
 
Join Date: Jul 2011
Really weird error

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?
xcodeNewbie is offline   0 Reply With Quote
Old Aug 14, 2011, 05:30 PM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
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.
jiminaus is offline   0 Reply With Quote
Old Aug 14, 2011, 06:46 PM   #3
xcodeNewbie
Thread Starter
macrumors member
 
Join Date: Jul 2011
Dealloced Planets

Thanks for your reply, but I already ran an NSLog test that confirmed the planets still exist.
xcodeNewbie is offline   0 Reply With Quote
Old Aug 14, 2011, 09:21 PM   #4
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by xcodeNewbie View Post
but I already ran an NSLog test that confirmed the planets still exist.
How did you do that? That NSLog code you posted above does not check whether the object exists.
jiminaus is offline   0 Reply With Quote
Old Aug 14, 2011, 09:55 PM   #5
RonC
macrumors regular
 
Join Date: Oct 2007
Location: Chicago-area
Quote:
Originally Posted by xcodeNewbie View Post
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.
RonC is offline   0 Reply With Quote
Old Aug 15, 2011, 04:47 AM   #6
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
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.
__________________
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Trying To Update ~ Weird Error MacFly69 OS X Mavericks (10.9) 30 May 25, 2014 07:26 PM
Weird Error...at a loss lastgoodbrother Mac OS X 10.7 Lion 3 Feb 11, 2013 07:50 AM
Weird mail error Forkjulle OS X 10.8 Mountain Lion 1 Oct 23, 2012 10:01 AM
Weird iCloud error. pankirk iCloud and Apple Services 1 Sep 5, 2012 03:19 PM
NSInvocation error, very weird Fuzej iPhone/iPad Programming 0 Jul 27, 2012 12:30 PM

Forum Jump

All times are GMT -5. The time now is 01:55 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC