Timer.fire error (bug?)

BlitzMax Forums/BlitzMax Programming/Timer.fire error (bug?)

SpaceAce(Posted 2006) [#1]
I'm having a problem with a program I am working on. I keep getting seemingly random "Unhandled Memory Exception Excpetion Error" crashes. Whenever they happen in a debug build, BLIde opens timer.bmx and highlights this line:

Function _TimerFired( timer:TTimer )
-->	timer.Fire
End Function


Here is the code I have that deals with timers:

  Field DrawTimer:TTimer, ProgressBarTimer:TTimer

  Method StartDrawTimer()
  	DrawTimer = CreateTimer(120)
  End Method

  Method StopDrawTimer()
  	If (DrawTimer) StopTimer(DrawTimer)
  End Method

  Method StartProgressBarTimer()
    ProgressBarTimer = CreateTimer(10)
  End Method

  Method StopProgressBarTimer()
    If (ProgressBarTimer) StopTimer(ProgressBarTimer)
  End Method


The error does not happen during the execution of any of the above code; instead, it happens after the timer is created (presumably when it fires). I cannot find any common thread with the crashes. They happen at different times when the program is doing different things under a variety of different circumstances.

I noticed this line in the documentation for StopTimer:
Once stopped, a timer can no longer be used.

I did not think that applied to creating a new time using the same variable. Does it?

SpaceAce


ziggy(Posted 2006) [#2]
Check the stack information provided by BLIde to know wich function or variable is calling the _TimeFired function, and ensure everything is as spected when this function is called.
If this function is called by a hook, be sure that everything is tratened well in the hook.


SpaceAce(Posted 2006) [#3]
Here's what I've been able to figure out so far:
BlitzMax seems to be trying to fire a trigger that has already been stopped. Apparently, sometimes when I call the StopTimer() function, the timer fires again, anyway. Since it seems so random, I am guessing the error occurs when the timer is Stop()ped but the timer.fire is already in progress or queued up to go. Maybe the timer is being Stop()ped between the instruction to fire the timer and the actual event emission...? Just speculation.

My workaround:
I created a StopTimer() function that waits for the timer to fire THEN immediately calls StopTimer(). This seems to have completely eliminated the memory error, but it also causes a delay while the StopTimer() loop waits for the timer or timers to fire.

SpaceAce


Fabian.(Posted 2006) [#4]
Maybe this helps on win32: Change "mod/brl.mod/timer.mod/timer.win32.c" on line 38 from
return timeSetEvent( 1000.0/hertz,0,timerProc,(DWORD)timer,TIME_PERIODIC );
to
return timeSetEvent( 1000.0/hertz,0,timerProc,(DWORD)timer,TIME_PERIODIC|TIME_KILL_SYNCHRONOUS );



GfK(Posted 2006) [#5]
I know this is a month-old thread but I've been having this problem myself recently. The debugger was next to useless in resolving it.

It seems that using too high a frequency for your timer can cause this problem. I was using 1000 for the sake of simplicity, and getting random errors all over the place.

Changed timer frequency to 5, problem has apparently been fixed.


H&K(Posted 2006) [#6]
If you set your timer to 1000Hrtz (ie every milli), it will sometimes "click" before the milli timer and sometimes after. So if for example all you do is see how long has passed since the last "Click" it will change between 0, 1, and 2 milli.
Obviously it isnt taking 2 milli (0r 0 milli), between clicks, its just that the milli is the smallest unit we have to measure it in.

This may not be the reason for you problems.


SpaceAce(Posted 2006) [#7]
In my situation, the faster timer was set to fire 120 times per second and the slower one to fire 10 times per second. The error (as far as I could determine) only happened when I shut the timers down then restarted them. The best I can figure is that the timer was firing after it had been deleted. Maybe a firing was already queued up before I deleted the timer. I can't really be sure but I'd sure like to see some sort of reliable solution to this problem.

SpaceAce


H&K(Posted 2006) [#8]
Maybe a firing was already queued up before I deleted the timer

Well I only ever stop timers from within whatever Function Ive told the Timer to run ( http://www.blitzbasic.com/codearcs/codearcs.php?code=1721 ), and Ive not come across this problem, basicly if I tell i to stop, it stops. However My main code isnt very CPU intensive, so Im assured of getting the Click before the next click, unless disc access or the like grabs the cpu

You have not posted anycode to show how you are reacting to the timers (ie While message loops etc), so I can only guess, but I would say that its very unlikly that you have taken to long to stop the slower timer, but the faster one, possibly