Jump to content

Memory issues


Recommended Posts

I was wondering how AutoIT deals with memory. My code is below and sometimes when I run it for long periods, I get a memory error I think from a memory leak.

HotKeySet("^{ESC}", "_Done") 
HotKeySet("^a", "_Start")
HotKeySet("^s", "_Stop")

$count = 0

_Stop()

Func _Stop()
    While 1     
        _hpCheck()
        _hungerCheck()
        Sleep(1000)     ; 
    WEnd
EndFunc

Func _Start()
MouseClick("left") ;makes sure the func starts right
    
    While 1
        _hpCheck()
        _hungerCheck()
        _hunt(7000, 0x18555a, 0)
        
        ; Takes about 4 seconds for Count to get to 10
        if $count > 3750 Then
            MouseClick("left", 980, 320, 1, 0)
            $count = 0
        EndIf

    WEnd
                 
EndFunc
    
Func _hunt($waitTime, $color, $sumHunt)
    
    if $sumHunt = 0 then
        $found = _huntCoord(260, 130, 715, 350, $color, $waitTime)      ; upper left
        $found = _huntCoord(716, 510, 1170, 752, $color, $waitTime)             ; lower right
        $found = _huntCoord(716, 130, 1170, 350, $color, $waitTime)             ; upper right
        $found = _huntCoord(260, 510, 715, 752, $color, $waitTime)      ; lower left
        if $found == 0 Then
            $count += 1
        Else 
            $count = 0
        EndIf
        
    else
        $found = _huntCoordSummon(260, 130, 715, 350, $color, $waitTime)  ; upper left
        $found = _huntCoordSummon(716, 510, 1170, 752, $color, $waitTime) ; lower right
        $found = _huntCoordSummon(716, 130, 1170, 350, $color, $waitTime) ; upper right
        $found = _huntCoordSummon(260, 510, 715, 752, $color, $waitTime)  ; lower left
        if $found == 0 Then
            $count += 1
        Else 
            $count = 0
        EndIf
    EndIf
EndFunc

Func _huntCoord($left, $top, $right, $bot, $color, $waitTime)
    _hpCheck()

    $c_MonsterHunt = PixelSearch($left, $top, $right, $bot, $color) ;Search for monster
    If Not @error And IsArray($c_MonsterHunt) Then
        MouseClick("left", $c_MonsterHunt[0], $c_MonsterHunt[1], 1, 0) ;Engage certain monster;  
        _sleepHp($waitTime, 5)
        _hungerCheck()
        Return 1
    EndIf
    _hungerCheck()
    Return 0
EndFunc

Func _huntCoordSummon($left, $top, $right, $bot, $color, $waitTime)
    _hpCheck()
    $c_MonsterHunt = PixelSearch($left, $top, $right, $bot, $color) ;Search for monster
    If Not @error And IsArray($c_MonsterHunt) Then
        Send("{SHIFTDOWN}")
        MouseClick("left", $c_MonsterHunt[0], $c_MonsterHunt[1], 1, 0) ;Engage certain monster;  
        sleep(500)
        Send("{SHIFTUP}")
        sleep($waitTime)
        _hungerCheck()
        Return 1
    EndIf
    _hungerCheck()
    Return 0
EndFunc

Func _sleepHp($time, $div)
    $wt = $time/$div
    While $div > 0
        sleep($wt)
        _hpCheck()
        $div = $div -1
    WEnd
EndFunc
             
Func _hpCheck()
    $c_HPBar = PixelSearch(300, 160, 430, 170, 0x4a4531, 0) ;Search HP bar for background color.
        If Not @error And IsArray($c_HPBar) Then                 
            Send("{F1 1}") ;Drink potion. 
            Sleep(1000)     
        EndIf    
EndFunc

Func _hungerCheck()
    $c_HungerBubble = PixelSearch(333, 125, 351, 134, 0xff0000) ;Search for hunger bubble.    
        If Not @error And IsArray($c_HungerBubble) Then                 
            Send("{F2 1}") ;Eat food.  
            Sleep(1500)         
        EndIf
    EndFunc


Func _Quit()
    MouseClick("left", 1173, 75, 1, 0)
    sleep(200)
    MouseClick("left", 861, 469, 1, 0)
    sleep(200)
    MouseClick("left", 669, 467, 1, 0)
    sleep(200)
    MouseClick("left", 700, 491, 1, 0)
EndFunc

Func _Done()
    Exit
EndFunc

I'm pretty new to AutoIT so I was wondering if someone could look through my code and see if I'm messing something up somehwere. Much appreciated!

Link to comment
Share on other sites

Two problems:

1. You expclicitly call '_Stop()' after setting it as a hotkey. When the hotkey is pressed, guess what happens?

2. _Start() is an endless loop, and _Stop() is too. So you're locked into one of those two functions indefinitely, but a press of the hotkey will start another endless loop linked to that hotkey.. which I imagine will continue to eat up memory (another press, another entry into the endless loop)

Suggestions:

1. Don't call _Stop explicitly. Do you really want to have the same function that's running being called again? Your function can just enter a While 1 / Sleep(1000) / Wend loop *outside* of the function

2. Create a boolean value (ex: $bStopPressed).

3. On entry to _Start(), check if $bStopPressed is False - if so, exit the function. Otherwise, go on to set it to False in _Start() BEFORE the loop, and check to see if its True within the loop (if so, exitloop/exit function)

3. On entry to _Stop(), check if $bStopPressed is True, if so, exit function. Otherwise, continue on, set it to True BEFORE the loop, check for False within the loop, and if it becomes False, exit the loop

*edit revised suggestion #1 hehe

Edited by Ascend4nt
Link to comment
Share on other sites

Hey thanks a lot! This seems like it'll do the trick!

Just another thing I'm curious about if I stop the script from running, does it cleanup all the memory? For example in my case, if I ran this, it started a bunch of endless loops, and then I stopped the script, would the memory still be allocated to those loops?

Link to comment
Share on other sites

Hey thanks a lot! This seems like it'll do the trick!

Just another thing I'm curious about if I stop the script from running, does it cleanup all the memory? For example in my case, if I ran this, it started a bunch of endless loops, and then I stopped the script, would the memory still be allocated to those loops?

Nah the basic functions will usually properly free up the memory they used after you exit.

The only things I've ever noticed behaviour like that with are filereads without fileclose's (or filereads compined with a hard crash) keeping me from editing said file and things created through ObjCreate.

And only because of my own bad coding.

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...