Jump to content

Variable used without being declared


Recommended Posts

Hi,

I am using yours example for an NT Service and it works fine, but sometimes when the computer is starting up and logging on I'll get the message Error: Variable used without being declared. It is very random and not consistent.

In the _Svc_Main() section I have justed added very basic stuff.

Any help is appreciated.

I haved added this section, which shouldn't cause a problem:

Func _ReduceMemory($i_PID = -1)
    
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    
    Return $ai_Return[0]
EndFunc;==> _ReduceMemory()
Link to comment
Share on other sites

The error message tells you the line and the variable which is not declared if the script is run from SciTE.

Can you post the complete message?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

The error message tells you the line and the variable which is not declared if the script is run from SciTE.

Can you post the complete message?

Hi Water,

The line is 3246 and doesn't exist. So I figure something strange is happening.

Most of the time I never get the error - just occassionally. I nonethless would like to resolve it.

Link to comment
Share on other sites

it might be in the UDF that your using since it's not directly in your code. or you could be using the syntax wrong. maby, im not 100% sure.

I thought about the UDF also. There is no such line :huh2:

I'll keep looking through the code. If the error consistently popped up I would understand, but it is random.

Link to comment
Share on other sites

The line number given from a compiled script is useless for troubleshooting where the problem is in the script, the numbers never match up. I would try running the script through Au3Check with the -d option and see if the variable issue shows up. The only other thing I can think to do, short of going through your script and the UDF and checking that each variable is properly declared, is using the Opt("Mustdeclarevars", 1) setting at the top of your script and testing it a LOT.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Most of the time I never get the error - just occassionally. I nonethless would like to resolve it.

What do you mean by "most of the time"?

If you run the same script from SciTe multiple times you sometimes get the error and sometimes you don't?

Do you use Obfuscator when compiling the script? If yes then there is a file scriptname_obfuscated.au3 which is then fed to the compiler.

Check this file to see which statement is wrong.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • 2 weeks later...

This may help.

I have noticed this issue in one of my projects:

I would get the intermittent error

"Variable used without being declared"

It was pointing to a line that was a declaration, or a blank line between functions.

After adding messages to narrow down the area in the code where there was a problem, I found that I had a declaration inside a branch, and was using it in another branch. I thought AutoIT would scan the complete function for declarations like in ms VB6 before running the code and that $arTokens only needed to be declared once, but that was not correct (and it looks like the syntax check made the same bad assumption). The code looked like this:

if ($Condition1) Then

Dim $arTokens = StringSplit ( $sTestFileBuff, @CRLF & $sTestInfo & "+Copies: 1 - 2" & @CRLF, 1 )

etc

ElseIf (Condition2) Then

$arTokens = StringSplit ( $sTestFileBuff, @CRLF & $sTestInfo & @CRLF, 1 )

etc

ElseIf (Condition3) Then

$arTokens = StringSplit ( $sTestFileBuff, @CRLF & $CurrentLine & @CRLF )

etc

Else

etc.

EndIf

The issue disappeared after I added "Dim " before $arTokens in each branch.

The declaration problem was not caught when I did the syntax check from inside the SciTE IDE (ctrl F5) though the line

Opt("MustDeclareVars", 1)

was at the top of the script.

I assume that the issue was intermittent because the code path that would be used is not always the same. In some paths, variables would be correctly initialized (if Condition1 is true in the example above), in others, they would not, and the error would come up.

Link to comment
Share on other sites

If it's a global variable, you should be declaring it at the top of the script, if it's a local variable you should be declaring it at the start of the function. Otherwise, you'll end up with the conditional declaration as you discovered. It's bad programming to conditionally declare a variable that way. It probably wasn't flagged as an undeclared variable due to the declaration after the first condition, because it comes before the other lines that use the same variable the other instances were assumed to be declared already. If you had put the Dim statement before the last conditional it probably would have been flagged.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

there are differing opinions about where to declare the variables, but this is not the right forum for that kind of discussion. The idea here was to explain a situation where the undeclared variable error would crop up.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...