Local variables should be automatically destroyed when you leave their context. The resources they used will either be freed and returned, or re-used.

Global variables will remain all the time that the KiXtart executable that they were defined in is running.

With "Explicit" on, you may get an error if you redeclare a variable. To avoid this, choose a naming convention for your global variables which ensures that you are not likely to have a name clash. I used the form "$_filename_variablename" for global variables. Check out the FAQ forum for recommendations.

If you include UDFs, libraries or a common file that defines constants, use a technique which ensures that the variables will only be declared once.

For example, if your UDF is stored in a file called myudf.kix, use something like the following:
Code:
;
; MYUDF() - demo UDF file.

; Ensure we are only included once.
If IsDeclared($_MYUDF_INCLUDED) Exit 0 EndIf

; Define included global
Global $_MYUDF_INCLUDED

; Define globals used by this UDF
Global $_MYUDF_Counter

; UDF definition.
Function MyUDF()
$_MYUDF_Counter=$_MYUDF_Counter+1
"You have called MyUDF "+$_MYUDF_Counter+" times."+@CRLF
Exit 0
EndFunction



The IsDeclared() check will ensure that your variables are only declared once. Note, the function itself will be redefined as that happens during the parse phase.