|
You last visited: Today at 19:43
Advertisement
AutoIt will quit to prevent stack overflow
Discussion on AutoIt will quit to prevent stack overflow within the AutoIt forum part of the Coders Den category.
08/16/2017, 00:59
|
#1
|
elite*gold: 0
Join Date: Jun 2012
Posts: 377
Received Thanks: 107
|
AutoIt will quit to prevent stack overflow
Hello, I have the following problem with my bot:
AutoIt will quit to prevent stack overflow
My script:
Code:
#include <imageSearch.au3>
HotKeySet("{F2}", "Play")
HotKeySet("{ESC}", "Fechar")
HotKeySet("{PAUSE}", "TogglePause")
Global $g_bPaused, $MBDx, $MBDy, $MapaBonusx, $MapaBonusy, $PIXEL, $MB, $MBx, $MBy
Func Play()
While 1
Global $MB = _ImageSearch('MB.bmp', 1, $MBx, $MBy, 0)
If $MB = 1 Then
MouseClick("left", $MBx, $MBy, 1, 10)
Sleep(17000)
Else
Return
EndIf
$PIXEL = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, 0xFF9900) ; NPC PIXEL
If IsArray($PIXEL) Then
Sleep(50)
MouseClick("left", Random(1717, 1804), Random(244, 331), 1, 3)
Sleep(50)
MouseClick("left", $PIXEL[0], $PIXEL[1])
EndIf
Send("c")
MouseClick("left", Random(0, 1542), Random(300, 808), 1, 3)
Send("n")
Sleep(50)
Send("e")
Sleep(50)
Send("f")
Sleep(50)
Send("n")
Sleep(50)
Send("e")
Sleep(50)
Send("f")
Sleep(50)
Send("n")
Sleep(50)
Send("e")
Sleep(50)
Send("f")
Sleep(50)
play()
WEnd
EndFunc ;==>Plcay
While 1
Sleep(100)
WEnd
Func Fechar()
Exit
EndFunc ;==>Fechar
Func TogglePause()
$g_bPaused = Not $g_bPaused
While $g_bPaused
Sleep(100)
ToolTip('Script is "Paused"', 0, 0)
WEnd
ToolTip("")
EndFunc ;==>TogglePause
I've already read a lot, and this problem is when calling functions from inside of functions and never allowing a function to fully complete and return, but in my case, i'm not calling functions.
I've read the article about Recursion in wiki
https://www.autoitscript.com/wiki/Recursion
but i dont understand what i have to do to solve this problem...
|
|
|
08/16/2017, 01:19
|
#2
|
elite*gold: 0
Join Date: Feb 2009
Posts: 1,137
Received Thanks: 573
|
Quote:
|
but in my case, i'm not calling functions.
|
take a look at the end of the play function:
Code:
Sleep(50)
Send("f")
Sleep(50)
play()
WEnd
EndFunc ;==>Plcay
Before the WEnd you are calling play().
PS: normally AutoIt should give you this line in the error message
|
|
|
08/16/2017, 01:46
|
#3
|
elite*gold: 0
Join Date: Jun 2012
Posts: 377
Received Thanks: 107
|
Quote:
Originally Posted by warfley
take a look at the end of the play function:
Code:
Sleep(50)
Send("f")
Sleep(50)
play()
WEnd
EndFunc ;==>Plcay
Before the WEnd you are calling play().
PS: normally AutoIt should give you this line in the error message
|
Hello, i've tried without the play() and i got the same error,
Is it possible to "erase" the stack? It seems that using the "return" I can "erase" this stack, how can I make it work? Thank you so much for helping
|
|
|
08/17/2017, 00:41
|
#4
|
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 167
|
Don't use while loop inside function if you want to pause your script later.
You should use boolean values to check when to work or not.
Code:
#include <imageSearch.au3>
HotKeySet("{F2}", "start")
HotKeySet("{ESC}", "Fechar")
HotKeySet("{PAUSE}", "TogglePause")
Global $g_bPaused=true
Global $MBDx, $MBDy, $MapaBonusx, $MapaBonusy, $PIXEL, $MB, $MBx, $MBy
Func start()
g_bPaused = false
EndFunc
Func Play()
Global $MB = _ImageSearch('MB.bmp', 1, $MBx, $MBy, 0)
If $MB = 1 Then
MouseClick("left", $MBx, $MBy, 1, 10)
Sleep(17000)
Else
Return
EndIf
$PIXEL = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, 0xFF9900) ; NPC PIXEL
If IsArray($PIXEL) Then
Sleep(50)
MouseClick("left", Random(1717, 1804), Random(244, 331), 1, 3)
Sleep(50)
MouseClick("left", $PIXEL[0], $PIXEL[1])
EndIf
Send("c")
MouseClick("left", Random(0, 1542), Random(300, 808), 1, 3)
Send("n")
Sleep(50)
Send("e")
Sleep(50)
Send("f")
Sleep(50)
Send("n")
Sleep(50)
Send("e")
Sleep(50)
Send("f")
Sleep(50)
Send("n")
Sleep(50)
Send("e")
Sleep(50)
Send("f")
Sleep(50)
EndFunc ;==>Plcay
While 1
if ($g_bPaused) then
Sleep(100)
ToolTip('Script is "Paused"', 0, 0)
else
ToolTip('')
Play()
EndIf
WEnd
Func Fechar()
Exit
EndFunc ;==>Fechar
Func TogglePause()
$g_bPaused = Not $g_bPaused
EndFunc ;==>TogglePause
|
|
|
08/17/2017, 04:16
|
#5
|
elite*gold: 0
Join Date: Jun 2012
Posts: 377
Received Thanks: 107
|
Using Boolean I have to call the function, yes? Is there any way to I call functions inside a function and don't get stack overflow?
|
|
|
08/17/2017, 11:42
|
#6
|
elite*gold: 100
Join Date: Apr 2008
Posts: 860
Received Thanks: 1,487
|
Quote:
Originally Posted by .Hyleex.
Is there any way to I call functions inside a function and don't get stack overflow?
|
Yes there is: Dont build infinite recursions! Or don't build recursions at all, because MOST of the time, they are completely unnecessary.
|
|
|
08/17/2017, 13:35
|
#7
|
elite*gold: 0
Join Date: Jun 2012
Posts: 377
Received Thanks: 107
|
Quote:
Originally Posted by florian0
Yes there is: Dont build infinite recursions! Or don't build recursions at all, because MOST of the time, they are completely unnecessary.
|
But if we consider it necessary to use recursion, how can I write the script so that I have no problems?
|
|
|
08/17/2017, 13:59
|
#8
|
elite*gold: 100
Join Date: Apr 2008
Posts: 860
Received Thanks: 1,487
|
Don't make them infinite. Make the recursion return before the stack overflows.
Also, most recursions can be replaced with a simple loop. To be more precise, all tail recursions can be resolved as loops (which is the case for your example).
|
|
|
08/17/2017, 22:45
|
#9
|
elite*gold: 0
Join Date: Jun 2012
Posts: 377
Received Thanks: 107
|
Quote:
Originally Posted by florian0
Don't make them infinite. Make the recursion return before the stack overflows.
Also, most recursions can be replaced with a simple loop. To be more precise, all tail recursions can be resolved as loops (which is the case for your example).
|
Hm... I can use return to prevent stack overflow, correct? I've read some texts about return but I haven't understand exactly how to use and how "return" works. Have a nice day!
|
|
|
08/17/2017, 23:39
|
#10
|
elite*gold: 100
Join Date: Apr 2008
Posts: 860
Received Thanks: 1,487
|
In general, "return" will leave a function; skipping what ever code is executing.
Lets say you have a simple pseudocode structure like this:
Code:
function DoMyStuff(coolnumber) {
if (coolnumber < 5) {
print "Cool Number is too small"
return
}
print "Cool Number is :" + coolnumber
}
The return inside will directly leave the function. The second print is not executed.
For recursions, there is something to keep in mind:
Each function call will use some memory on the stack, for local variables, for parameters and the return. This is true for most (if not all) compiled languages, and also some script languages.
Code:
function MyStuff() {
print "Hello World!"
MyStuff() // <- recursive call
}
With each call to MyStuff, the stack ascends by at least the return address. If we run that program, it will run forever. But sadly, the size of the stack is limited. With each recursion, the stack gets fuller and fuller because the function never returns. And at some point, there is no memory left -> Stack overflow
Lets look at a different example: A formular for the greatest common divisor of two numbers based on Euklid.
Code:
function gcd(a, b) {
if (b == 0) {
return a
}
return gcd(b, a % b)
}
The recursion goes on and on, but at some point, b will become 0. If this occurs, the function will return without calling itself again. At this point, the recursion stops. The program descends through the stack.
There is still a chance that this function will overflow the stack. But unlike the first example, which had a 100% chance to overflow the stack, this one will only fail for really large numbers of recursions, aka. really large numbers as input.
|
|
|
08/18/2017, 00:14
|
#11
|
elite*gold: 0
Join Date: Jun 2012
Posts: 377
Received Thanks: 107
|
I think I understand...
in your first example, when you exit the function, it will return to the previous function who called DoMyStuff(), correct? so, if you create a "main" function who call every function, and every function have a break condition, (if $something = 1 then return) so, it will return to main function, and main function will decide what function to call,
will this solve the problem? i've read this here:
Can I solve the problem doing this?
Thank you so much for your help!
|
|
|
08/18/2017, 23:58
|
#12
|
elite*gold: 100
Join Date: Apr 2008
Posts: 860
Received Thanks: 1,487
|
Yes, this seems to be the same problem as yours.
I still think you don't need that recursion. Recursions are a special pattern that is only useful for solving certain problems (like calculating the fibonacci number). Your program does not make use of the recursion (does not pass data, recursion has no real impact on the codeflow. The recursion is, in your case, only a simple loop.
Like my example:
Code:
function MyStuff() {
print "Hello World!"
MyStuff() // <- recursive call
}
is just another form of
Code:
function MyStuff() {
while (true) {
print "Hello World!"
}
}
|
|
|
08/21/2017, 02:56
|
#13
|
elite*gold: 0
Join Date: Jun 2012
Posts: 377
Received Thanks: 107
|
Quote:
Originally Posted by florian0
Yes, this seems to be the same problem as yours.
I still think you don't need that recursion. Recursions are a special pattern that is only useful for solving certain problems (like calculating the fibonacci number). Your program does not make use of the recursion (does not pass data, recursion has no real impact on the codeflow. The recursion is, in your case, only a simple loop.
Like my example:
Code:
function MyStuff() {
print "Hello World!"
MyStuff() // <- recursive call
}
is just another form of
Code:
function MyStuff() {
while (true) {
print "Hello World!"
}
}
|
i wanna understand how recursion works, and how i can avoid this problem for future projects, thank you!
|
|
|
 |
Similar Threads
|
[Help] [AutoIT] Imagesearch stack overflow
08/26/2015 - AutoIt - 6 Replies
Hey everyone I'm playing around with auto it and I'm new. I haven't realy searched a lot for my problem but I wrote a skripts that is running. Its finding images clicking on them and everythin that I want but its running like 20-30 minutes or less or more and its stops, the scripts just exit and in the script I get this error:
"C:\Program Files (x86)\AutoIt3\Include\ImageSearch.au3" (34) : ==> Recursion level has been exceeded - AutoIt will quit to prevent stack overflow.:
Func...
|
Stack Overflow
06/03/2015 - AutoIt - 6 Replies
Hallo,
ich programmiere gerade Tic Tac Toe mit GUI und einer random KI. Leider kommt bei mir immer
Recursion level has been exceeded - AutoIt will quit to prevent stack overflow
ich weiß, dass das ist weil sich die Funktion so oft aufgerufen hat. Ich sehe aber keine Lösung. Könntet ihr mir vielleicht sagen, was an meinem Programm falsch ist?
;TIC TAC TOE;
#include<GUIConstantsEx.au3>
Global $aSpielfeldbutton
Global $Check1 = 0
Global $Check2 = 0
|
Hilfe, Stack Overflow!
01/29/2014 - AutoIt - 6 Replies
Hallo ich programmiere grade mein ersten bot mit "ImageSearch" und habe das Problem das nach einer weile die Fehlermeldung stack overflow kommt und ich weis nicht wie ich das verhindern soll.
Ich arbeite mit autoit seit 2 Wochen und das ist mein Ergebnis.
Ich hoffe das ihr mir helfen könnt.
Danke.
Der autoit code
|
Voten für Deutsches Stack Overflow
02/14/2013 - Web Development - 2 Replies
Die meisten bzw. viele von euch kennen sicher das Stack Overflow. Es gibt (schon sehr lange) eine möglichkeit zum Voten für ein Deutsches (: Wäre natürlich sehr schick wenn diese in die Beta kommt. Dafür braucht man aber viele Unterstützer. Also Votet (Committen auf der linken seite) (x
Stack Overflow (in German) - Area 51 - Stack Exchange
|
Recursion level has been exceeded - AutoIt will quit to prevent stack overflow.
01/20/2013 - AutoIt - 4 Replies
Hallo Leute,
ich hab das folgende Problem mit meinem Bot:
Nach ca. 4-5 Std. Laufzeit meines Bots bekomme ich folgende Meldung...
"Recursion level has been exceeded - AutoIt will quit to prevent stack overflow."
Das ganze an völlig unterschiedlichen Stellen, es ist also wirklich die Addition die das Problem hervorruft und nicht eine bestimmte Funktion etc.
|
All times are GMT +1. The time now is 19:43.
|
|