Sign in to follow this  
Followers 0
Dieuz

Stack Overflow

13 posts in this topic

#1 ·  Posted (edited)

I know there's alot of topic about this subject on the forum but no one seem to help me so I made this one. I just made a new script and I'm getting this Stack Overflow error. Sometime, the script can run fine during 6-7 hours without getting this error but sometime it happen after 15 min. I don't know exactly what to change to my script to correct this problem. Here's the basic of my script. There's alot of loops because it's a bot. It has to be able to run during an infinitive time.

NEW SCRIPT, scroll down

Edited by Dieuz

Share this post


Link to post
Share on other sites



I know there's alot of topic about this subject on the forum but no one seem to help me so I made this one. I just made a new script and I'm getting this Stack Overflow error. Sometime, the script can run fine during 6-7 hours without getting this error but sometime it happen after 15 min. I don't know exactly what to change to my script to correct this problem. Should I remove some functions? Should I remove the CALL on the function Restart but change it for what? Should I remove CALLs at all? Here's the basic of my script. There's alot of loops because it's a bot. It has to be able to run during an infinitive time.

;Hotkeys
HotKeySet("{HOME}", "Terminate") ;
HotKeySet("{F2}", "Start") ; 
HotKeySet("{F4}", "Pause") ; 

;Variables

Global $Paused
$ColorAttack = Pixelgetcolor(837,464)
$ColorVictory = Pixelgetcolor(402,169)
$Color5wins = Pixelgetcolor(595,387)
$Trophyswon = 0
$ColorBug = Pixelgetcolor(840,467)
While 1
    sleep(100)
Wend

Func Start()

;Stuff here

Call ("Restart")
EndFunc

Func Restart()
    
;Stuff here

;5 WINS
If $Color5wins ="5911585" Then
    Call ("Winner")
EndIf

;stuff here

Call ("Restart")
EndFunc
Func Winner()

;stuff here

Call ("Restart")
EndFunc

Func Pause()

;stuff here
    
EndFunc

Func Terminate()
  Exit(0)
  EndFunc
oÝ÷ Ø-ëæ¬jëh×6Restart()

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I tought I had corrected it but I still have this stack overflow problem.

While 1
    sleep(100)
Wend

Func Start()

;stuff here, this function only happen once

Restart()
EndFunc

Func Restart()

While 1
    

Sleep(1500) ***********************************


If    Then
; stuff here    

Do

;stuff here

Until 
    
;stuff here

Else
    
;stuff here

If  then    ; happen like 1 time out of 1000
Restart()
Else


Do    
;stuff here

Until 

;stuff here

EndIf
EndIf
Wend
EndFunc


Func Pause()
;stuff here
Endfunc
    
Func Terminate()
  Exit(0)
  EndFunc

*********************** = where im getting the stack overflow error

Maybe there's something wrong in my script but I cannot figure it out. I dont understand why there's recursion because there's no call at all ( except one that is almost never used). Also, I dont stack any variables.

Edited by Dieuz

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I tought I had corrected it but I still have this stack overflow problem.

CODE
While 1
    sleep(100)
Wend

Func Start()

;stuff here, this function only happen once

Restart()
EndFunc

Func Restart()

While 1
    

Sleep(1500) ***********************************
If    Then
; stuff here    

Do

;stuff here

Until 
    
;stuff here

Else
    
;stuff here

If  then    ; happen like 1 time out of 1000
Restart()
Else
Do    
;stuff here

Until 

;stuff here

EndIf
EndIf
Wend
EndFunc
Func Pause()
;stuff here
Endfunc
    
Func Terminate()
  Exit(0)
  EndFunc
*********************** = is where im getting the stack overflow error

Maybe there's something wrong in my script but I cannot figure it out.

What part of "The problem is that Restart calls itself" did you not understand? Every time a function calls itself, it consumes part of the stack. That memory isn't returned to the system until the function exits. If you never exit the function by constantly looping within the function, you burn up the stack. Edited by PaulIA

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I will try to avoid calling. If the function constantly loop with While/Do/For does it consume the stack?

If I do something like that, does it consume part of the stack?

Func Restart()

If blablablabla then
Test()
EndIf

EndFunc

Func Test()

Restart()

EndFunc
Edited by Dieuz

Share this post


Link to post
Share on other sites

I will try to avoid calling. If the function constantly loop with While/Do/For does it consume the stack?

If I do something like that, does it consume part of the stack?

Func Restart()

If blablablabla then
Test()
EndIf

EndFunc

Func Test()

Restart()

EndFunc
That will do the same

Share this post


Link to post
Share on other sites

That will do the same

What will consume stack? While/Do/For or the function I just wrote?

Share this post


Link to post
Share on other sites

What will consume stack? While/Do/For or the function I just wrote?

Look at this example http://www.autoitscript.com/forum/index.ph...st&p=263863

This is the type of thing you will need to do.

Your functions must be able to exit and return, if they just keep calling each other then they never return and your script will bomb out

Share this post


Link to post
Share on other sites

Thx for the link Chris!

So If I am not calling at all, I should not get any recursion error right?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

It's been stated several times here, and several times in other topics that you have created. Take the advice that people are giving you and don't have Restart() call itself, ie. the following is recursive and will give you a stack overflow error:

Func Restart()
    ;blah blah
    Restart()
EndFunc

Learn to write loops to do what you desire, recursion is very rarely needed. Please stop multi-posting this all over the forums. Take a look at some of the examples/links people have posted.

Edit: typo

Edited by mikehunt114

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]

Share this post


Link to post
Share on other sites

Thx for the link Chris!

So If I am not calling at all, I should not get any recursion error right?

Here it is simplified, run it and you will see what it does

$start = 1
$Restart = 2
$pause = 3
$terminate = 4

$job = Start()

While 1
    
    Switch $job
        
    Case $start
        $job = Start()
    Case $Restart
        $job = Restart()
    Case $pause
        $job = Pause()
    Case $terminate
        $job = Terminate()
        
    EndSwitch
        
WEnd


Func Start()

;stuff here, this function only happen once
Msgbox (0,"","Start function",2)
Return $Restart

EndFunc

Func Restart()
    
$ans = Msgbox(4,"","Do you want to do some more?")
If $ans = 6 then 
    Return $start
Else
    Return $terminate
EndIf


EndFunc


Func Pause()
;stuff here
Endfunc
    
Func Terminate()
    Msgbox (0,"","OK Bye")
  Exit(0)
EndFunc

Share this post


Link to post
Share on other sites

Why are you using functions so much? It would be easier, and less recursive, if you would just make a main loop and use proper conditionals, it will not throw stack overflows like that.

Share this post


Link to post
Share on other sites

I'm using one main function, and 3 small one ( Start,Pause,Exit)

HotKeySet("{HOME}", "Terminate") ; Terminates the Script when HOME is pressed
HotKeySet("{ENTER}", "Start") ; Run the Script when ENTER is pressed
HotKeySet("{SPACE}", "Pause") ; Pause the Script when SPACE is pressed

Share this post


Link to post
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
Sign in to follow this  
Followers 0