OnMessage() Cause temporary variables to empty Topic is solved

Report problems with documented functionality
feiyue
Posts: 162
Joined: 08 Aug 2014, 04:08

OnMessage() Cause temporary variables to empty

29 Jan 2019, 18:37

When I used OnMessage ()+SetTimer, I found that the variables of the subroutine inside the function were confusing.
Look at the following example. Why is the value of variable “i” empty?

Code: Select all


Gui, Show, w500 h300
OnMessage(0x200, "WM_MOUSEMOVE")
return

WM_MOUSEMOVE() {
  SetTimer, Test, -100
  return

  Test:
  i=1
  Loop 10 {
    Sleep, 100
    j=1
  } Until (i!=j)
  MsgBox, %i% - %j%
  return
}

User avatar
kczx3
Posts: 858
Joined: 06 Oct 2015, 21:39

Re: OnMessage() Cause temporary variables to empty

29 Jan 2019, 20:04

Labels inside functions is just a bad idea anyways...
Helgef
Posts: 3867
Joined: 17 Jul 2016, 01:02
Contact:

Re: OnMessage() Cause temporary variables to empty  Topic is solved

30 Jan 2019, 01:16

When a function returns, its non-static variables are made blank.
docs wrote:subroutines should use only static and global variables (not locals) if their function is ever called normally. This is because a subroutine thread that interrupts a function-call thread (or vice versa) would be able to change the values of local variables seen by the interrupted thread. Furthermore, any time a function returns to its caller, all of its local variables are made blank to free their memory.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage() Cause temporary variables to empty

30 Jan 2019, 10:58

feiyue wrote:
29 Jan 2019, 18:37
Yha, it seems that "labels" inside functions are not recommended in some situations!

You can use the below alternative:

Code: Select all

Gui, Show, w500 h300
OnMessage(0x200, Func("WM_MOUSEMOVE").Bind("OnMessage_Call"))
return

guiclose:	;________________
exitapp


WM_MOUSEMOVE(Options := "") {	;_______________________________

	if (Options = "OnMessage_Call")
	{
		;msgbox, OnMessage_Call Test

	SetTimer, % A_ThisFunc, -100

	return
	}

  i=1
  Loop 10 {
    Sleep, 100
    j=1
  } Until (i!=j)
  MsgBox, %i% - %j%

}
just me
Posts: 6432
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: OnMessage() Cause temporary variables to empty

07 Mar 2019, 03:58

Helgef wrote:
30 Jan 2019, 01:16
When a function returns, its non-static variables are made blank.
So
  1. Code: Select all

    WM_MOUSEMOVE() {
       Static c, i, j
       SetTimer, Test, -100
       Return
    
       Test:
       i := 1
       Loop 10 {
          Sleep, 100
          c := A_Index
          j := 1
       } Until (i != j)
       MsgBox, %i% - %j% - %c%
       Return
  2. It isn't a bug!
feiyue
Posts: 162
Joined: 08 Aug 2014, 04:08

Re: OnMessage() Cause temporary variables to empty

08 Apr 2019, 15:45

Yes, I didn't realize before that subroutines in functions would be affected by this.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 9 guests