Page 1 of 1

Possible bug with SetTimeout()

Posted: Sat Apr 17, 2021 9:42 pm
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?

Re: Possible bug with SetTimeout()

Posted: Sat Apr 24, 2021 2:43 pm
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)