Possible bug with SetTimeout()

Report any Hollywood bugs here
Post Reply
User avatar
mrupp
Posts: 66
Joined: Sun Jan 31, 2021 7:44 pm
Location: Switzerland
Contact:

Possible bug with SetTimeout()

Post by mrupp »

I encountered some strange behaviour concerning SetTimeout(). Check out the following example:

Code: Select all

@REQUIRE "RapaGUI", {Link = True}
@APPTITLE "SetTimeout-Test"

m_timeoutID = Nil

Function p_EventFunc(msg)
	Switch msg.ID
	Case "setT":
		m_timeoutID = SetTimeout(Nil, p_testTimeout, 2000)
	Case "setT2":
		m_timeoutID = SetTimeout(Nil, p_testTimeoutWithUserData, 2000, "test")
	Case "clearT":
		ClearTimeout(m_timeoutID)
		moai.DoMethod("log", "insert", "bottom", "SetTimeout cleared")
		m_timeoutID = Nil
	EndSwitch
	moai.DoMethod("log", "jump", "bottom")
EndFunction

Function p_testTimeout()
	moai.DoMethod("log", "insert", "bottom", "p_testTimeout() called")
	moai.DoMethod("log", "jump", "bottom")	

	m_timeoutID = SetTimeout(Nil, p_testTimeout, 2000)
EndFunction

Function p_testTimeoutWithUserData(data)
	moai.DoMethod("log", "insert", "bottom", "p_testTimeoutWithUserData() called, userData: " .. data.userData)
	moai.DoMethod("log", "jump", "bottom")

	m_timeoutID = SetTimeout(Nil, p_testTimeoutWithUserData, 2000, data.userData)
EndFunction

InstallEventHandler({RapaGUI = p_EventFunc})

moai.CreateApp([[<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
	<window id="mainWindow" title="SetTimeout">
		<vgroup>
			<hgroup>
				<button id="setT">SetTimeout</button>
				<button id="setT2">SetTimeout with userData</button>
			</hgroup>
			<hgroup>
				<button id="clearT">ClearTimeout</button>
			</hgroup>
			<listview id="log" >
				<column/>
			</listview>
		</vgroup>
	</window>
</application>
]])

Repeat
	 WaitEvent
Forever
It looks like this:
Image

Both buttons call a function using SetTimeout which again calls itself using SetTimeout. So this is kind of a self-renewal-SetTimeout (I know you could use SetInterval() for this, but that's not the point here). The difference between the two is, that with the 2nd button the string "test" is passed on as userData.
Running the script and clicking "SetTimeout with userData", everything works just fine. Also if clicked and cleared multiple times.
BUT if you run the script and click "SetTimeout" first, then clearing the timeout and clicking "SetTimeout with userData" next, the function cannot call itself again using SetTimeout and the following error occurs:
Image

It seems to have something to do with passing userData to SetTimeout after having used SetTimeout without any userData.
Maybe a bug that needs some attention?

User avatar
airsoftsoftwair
Posts: 4611
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Possible bug with SetTimeout()

Post by airsoftsoftwair »

Yes, definitely a bug. It's fixed now.

Code: Select all

- Fix [SDK]: Timer states could get messed up when using SysBase.RunTimerCallback() to run a timer callback (which RapaGUI 2.0 does, for example) 

Post Reply