NSTimer lost in time?

This code :

	on startTimer()
		if gChrono is not missing value then gChrono's invalidate()
        set gChrono to current application's NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1, me, "adjustTime:", missing value, true)
		set my gMaximalTime to (gSecondsPerForm as integer) * gFormNumber
		set my gRemainingTime to gMaximalTime
	end startTimer
	
	on adjustTime_(aTimer)
		set my gRemainingTime to gRemainingTime - 1
		if gRemainingTime = 0 then correctNow_(me)
	end adjustTime_

    on correctNow_(sender)
		if gChrono is not missing value then gChrono's invalidate()
         -- correction
    end

gives the error
GC "operation on unregistered thread. Thread registered implicitly. Break on auto_zone_thread_registration_error() to debug.

I don’t know what is wrong here, as the docs say:

so “theTimer” should be equal to my gChrono property. or not?

I’ve gotten that error before, and it doesn’t crash the app or anything – I’m not sure that there is anything you can do about that error message. Is your app crashing? I’m not sure whether there is any problem invalidating the timer in another method that’s not the selector – it might be better to invalidate it in the adjustTime method, and then call correctNow to do whatever you need to do there.

Ric

Hi Ric,

No, the app is not crashing – I just dislike error messages, even if they don’t hurt.

My problem is – the correctNow_ method is also called by a button, which allows the user to interrupt the countdown if he has finished before time reaches zero, and if he wants to get the correction at that time. Note that if I use aTimer’s invalidate() I get no error. But hey, what if you can’t trust the docs? Maybe this is another coercion problem? I don’t know if you get this error in using Objective-C.

Regards,

I ran this, and got no error messages:

on applicationWillFinishLaunching_(aNotification)
            if gChrono is not missing value then gChrono's invalidate()
            set gChrono to current application's NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1, me, "adjustTime:", missing value, true)
            --set my gMaximalTime to (gSecondsPerForm as integer) * gFormNumber
            set my gRemainingTime to 6 --gMaximalTime
	end applicationWillFinishLaunching_
    
    on adjustTime_(aTimer)
        set my gRemainingTime to gRemainingTime - 1
        log "got here"
        if gRemainingTime = 0 then 
            log "got to 0"
            correctNow_(me)
        end if
    end adjustTime_
    
	on correctNow_(x)
        gChrono's invalidate()
        log "correcting"
    end

So, I don’t know why you’re getting that error – it seems to me that it’s somewhat random. I don’t think it has anything to do with ASOC vs. objective-C. I don’t know if it has something to do with how you’re calling startTimer() vs me doing it in applicationWillFinishLaunching?

Ric

Ric,

I quitted Xcode and relaunched it, and this time I got no error. So you were right, it is something “random” – maybe Xcode was in an unstable state after other errors, maybe another application had interfered, I don’t know.

Mmh, I posted a bit too fast, maybe. Sorry for for the inconvenience. :confused:

Regards,

Ric,

I repeatedly get some errors with my threads when the app is running on its own. It must have something to do with garbage collection. So I solved the problem as well:

  1. I added a property to act as a flag:
  1. I create one unique NSTimer in the applicationWillFinishLaunching_ method:
  1. I set and reset my flag when I want, anywhere in the application:
  1. The method fired by the timer just decreases the value if the chronometer is running :

Now everything is running fine. After all, in real life there is only one time (given by the NSTimer) and you just press on the buttons of your chronometer (setting the flag property). You don’t throw away your watch each time after getting the time, do you?

This was just to keep you informed.

Regards,