Data leaking into function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DataLife
Posts: 460
Joined: 29 Sep 2013, 19:52

Data leaking into function

24 Apr 2021, 06:32

Please excuse my terminology. I am sure I am calling things by the wrong names.

I always thought the only way data can get into a function is thru through calling the function with parameters. But I stumbled on another way.

I created the script and function below to demonstrate the issue. The function is not meant to do anything important.

The data "three" has made it into my function without being included in the calling parameters.

I always thought of a function like a sandbox. Completely separate of my script. What I do outside the function with variables is safe along as they are not declared global inside the function.

So I was surprised to see data from outside my script get into my function then my function returned that data when I called the function. But I suppose in this case it was not data from a variable that leaked into my function but data from an array.

I fixed the issue in my main script by not using the same name for the array outside and inside the function. I could also clear the array "variables" outside my function when they are no longer needed but in this case I was using my array "variables" and my function at the same relative time.

I searched the forum and found this from 12 years ago. If what is described here is what is happening to my function then this must be from design because it has not been fixed.
https://autohotkey.com/board/topic/39851-loop-makes-local-array-outside-function-in-public-subroutine/

Code: Select all

;
VariableSeperateOfFunction = one two three four
StringSplit,OutputArray,VariableSeperateOfFunction, %A_Space%

Var1 = apple red
var := ReverseVar(Var1)
msgbox,,%a_linenumber%,  % var

ReverseVar(VarName)
 { 
  
  StringSplit,OutputArray,VarName,%a_space% 
  OutPutArray0++
  Loop 3
   {
      X1 := ( OutPutArray%a_index% a_space X1 )
      msgbox %X1%
   }
   return  X1
 }
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Data leaking into function  Topic is solved

24 Apr 2021, 06:44

Dynamic variables such as though with “pseudo” arrays are resolved to globals at least under certain conditions but maybe all the time. I’d recommend not using dynamic variable names like that and use actual arrays.
User avatar
DataLife
Posts: 460
Joined: 29 Sep 2013, 19:52

Re: Data leaking into function

24 Apr 2021, 06:47

kczx3 wrote:
24 Apr 2021, 06:44
Dynamic variables such as though with “pseudo” arrays are resolved to globals at least under certain conditions but maybe all the time. I’d recommend not using dynamic variable names like that and use actual arrays.
Which variable is dynamic?

Edit...
Upon further review...OutPutArray%a_index% is a dynamic variable.

In my original function I was using OutputArray0 to know how many loops

I could use Force-local mode to fixed the issue easliy.
Last edited by DataLife on 24 Apr 2021, 06:59, edited 1 time in total.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Data leaking into function

24 Apr 2021, 06:56

Code: Select all

MsgBox, % reverse("apple red this")

reverse(str) {
 For index, word in StrSplit(str, " ")
  rev := word " " rev
 Return Trim(rev)
}
User avatar
DataLife
Posts: 460
Joined: 29 Sep 2013, 19:52

Re: Data leaking into function

24 Apr 2021, 07:05

mikeyww wrote:
24 Apr 2021, 06:56

Code: Select all

MsgBox, % reverse("apple red this")

reverse(str) {
 For index, word in StrSplit(str, " ")
  rev := word " " rev
 Return Trim(rev)
}
thanks for the suggestion but the function I posted is only an example of the issue and your code does not work with my original function in my original script.

I am passing a variable that has an unknown number of "elements" to the function. Sometimes 3, sometimes 5 and I never know how many until I do StringSplit. Plus I am not simply reversing the entire string. I am taking the last word and putting it first and leaving the rest the same. Then in another function I am taking the first word and moving it to the end and leaving the rest the same.

My function is working perfectly if I avoid "data leaking into my function"
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Data leaking into function

24 Apr 2021, 07:09

Your use case can still utilize StrSplit instead of StringSplit
User avatar
DataLife
Posts: 460
Joined: 29 Sep 2013, 19:52

Re: Data leaking into function

24 Apr 2021, 07:28

kczx3 wrote:
24 Apr 2021, 07:09
Your use case can still utilize StrSplit instead of StringSplit
All of these suggestions are how to avoid data leaking into my function.

I already know how to do that. I was looking to find out why this was happening and if it was intentional.

You showed me the reason is a dynamic variable.
I looked up dynamic variable and functions and found that this is intended behavior.

I don't know much about functions and arrays and why that it is intended but it is.

The help file gave a solution, "Force-local mode"

thanks for your help. I am marking this as resolved.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Data leaking into function

24 Apr 2021, 07:44

Good! Interesting issue; I was unaware of it. Thanks for pointing it out.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 388 guests