AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

In to deep? - Recursion crashes AHK
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Sun Aug 09, 2009 11:29 pm    Post subject: In to deep? - Recursion crashes AHK Reply with quote

this little script crashes ahk on my system possible due to recursive use of a function:
(it is supposed to create all possible permutations of a 255-char long string containing all 255 ASCII-Chars)
Code:
SetBatchlines -1
#NoEnv

loop, 255
   baselist .= Chr(A_Index)

makelists(baselist, "")
Tooltip
Listvars
MsgBox Done. %list_idx% strings created.

makelists(baselist, newlist)
{
   loop, parse, baselist
   {
      thischar := A_Loopfield
      StringReplace, newbaselist, baselist, %thischar%,, All
      new_newlist := newlist . thischar
      if (newbaselist = "")
      {
         newlist(new_newlist)
      }
      else
      {
         makelists(newbaselist, new_newlist)   
      }
   }
   return
}

newlist(newlist)
{
   global
   SetFormat, float, 3.0
   list_idx ++
   codelist_%list_idx% := newlist
   tooltip % list_idx
   return   
}

Does this happen on other systems, too? (please test)
Has anyone an idea for a work-around or knows a better way?

Edited the script to enhance testing comfort Wink


Last edited by Zed Gecko on Mon Aug 10, 2009 12:48 am; edited 1 time in total
Back to top
View user's profile Send private message
lilalurl.T32



Joined: 17 May 2007
Posts: 391
Location: Titan

PostPosted: Mon Aug 10, 2009 12:34 am    Post subject: Reply with quote

Seems to work for me if this the expected result:


No crashing at all (XP SP3, French version)
________
Love Forum


Last edited by lilalurl.T32 on Sun Mar 13, 2011 6:44 am; edited 1 time in total
Back to top
View user's profile Send private message
hugson



Joined: 07 Apr 2007
Posts: 12

PostPosted: Mon Aug 10, 2009 12:36 am    Post subject: Reply with quote

Works fine for me using Windows XP SP3 and AHK 1.0.48.3!
_________________
Regards,

Paul
Back to top
View user's profile Send private message Send e-mail
Z_Gecko
Guest





PostPosted: Mon Aug 10, 2009 12:41 am    Post subject: Reply with quote

Oh sorry, i was inacurate.
It crashes after the Msgbox is shown.

At the end, it should show a long list of variables via listvars.
Could you please uncomment the ; tooltip % list_idx - line to see if the script is working. (Tooltip should show the amount of created strings).

btw. i´m running W2K SP4
Back to top
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Mon Aug 10, 2009 12:51 am    Post subject: Reply with quote

i changed the script above to not show the misleading MsgBox and have the Tooltip display the number of generated strings. MsgBox is shown when done.
please test again.
Back to top
View user's profile Send private message
lilalurl.T32



Joined: 17 May 2007
Posts: 391
Location: Titan

PostPosted: Mon Aug 10, 2009 2:11 am    Post subject: Reply with quote

Well, nothing happens: the tray icon appears and when you go to click on it it disappear. Neither tooltip nor msgbox is displayed.

No error message (if this is what you mean by crash).
________
Silversurfer Vaporizer


Last edited by lilalurl.T32 on Sun Mar 13, 2011 6:44 am; edited 1 time in total
Back to top
View user's profile Send private message
Z_Gecko
Guest





PostPosted: Mon Aug 10, 2009 2:51 am    Post subject: Reply with quote

i get an errormessage actually:
Quote:
Programmfehler
AutoHotkey.exe hat Fehler verursacht und wird geschlossen.
Starten Sie das Programm neu.

Ein Fehlerprotokoll wird erstellt.

roughly translated: AHK caused an error and will be closed. Restart the program. An errorlog will be created.
Back to top
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Mon Aug 10, 2009 4:24 am    Post subject: Reply with quote

Were you out of your mind? Open the task manager and observe how much consumed memory is increased.
Back to top
View user's profile Send private message
nick



Joined: 24 Aug 2005
Posts: 549
Location: Berlin / Germany

PostPosted: Mon Aug 10, 2009 5:22 am    Post subject: Reply with quote

Moin Z_Gecko,

255! (Fakultät/factorial) Wink
_________________
nick Wink
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Mon Aug 10, 2009 10:03 am    Post subject: Reply with quote

this little script crashes ahk and my system Sad
Code:
shutdown 12

Sorry. Laughing
Back to top
View user's profile Send private message Visit poster's website
Murp|e



Joined: 12 Jan 2007
Posts: 531
Location: Norway

PostPosted: Mon Aug 10, 2009 11:44 am    Post subject: Reply with quote

A tray icon pops up and then a tray icon pops down. So to speak.
Back to top
View user's profile Send private message Visit poster's website
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Mon Aug 10, 2009 2:47 pm    Post subject: Reply with quote

Quote:
Were you out of your mind? Open the task manager and observe how much consumed memory is increased.
Well here the script uses no more than 10MB memory before it crahes. So i assumed the crash is caused by the recusrsion and not by excessive memory usage.
Quote:
255!
I am aware that final memory usage would exceed any physical possible amount. And i was prepared for a crash later on, but here it crashes instantly; it does not even pass through to the end of the first recursion which should only only use about 40MB if i estimate it right.

i modified the script a bit, so it does not keep the results in memory. But it still crashes almost instantly, it seems it does only reach a recursion-depth of 85 and crash with a memory use of around 7.8 MB:
Code:
SetBatchlines -1
#NoEnv

loop, 255
   baselist .= Chr(A_Index)

makelists(baselist, "")
Tooltip
Listvars
MsgBox Done. %list_idx% strings created.

makelists(baselist, newlist)
{
   loop, parse, baselist
   {
      global rec_depth
      thischar := A_Loopfield
      StringReplace, newbaselist, baselist, %thischar%,, All
      new_newlist := newlist . thischar
      if (newbaselist = "")
      {
         rec_depth := 0
         Tooltip, %rec_depth%, 0, 40, 2
         newlist(new_newlist)
      }
      else
      {
         rec_depth ++
         Tooltip, %rec_depth%, 0, 40, 2
         makelists(newbaselist, new_newlist)   
      }
   }
   return
}

newlist(newlist)
{
   global list_idx
   SetFormat, float, 3.0
   list_idx ++
   MsgBox % newlist
   tooltip %list_idx%, 0, 0, 1
   return   
}


I´m still clueless.

btw: the point of all this was to try an brute-force attack on the encryption this script from nick provides.

Edit: i created a much simpler recursive function that does not bloat the memory that much just for testing:
Code:
MsgBox % rec(0, 1000)
return
rec(deep, end)
{
   Tooltip %deep%
   if (deep = end)
      return "done"
   else
      return rec(deep + 1, end)
}
it crashes at 662 recursions.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Mon Aug 10, 2009 5:11 pm    Post subject: Reply with quote

I suspect a stack overflow then, but need to inspect the source code to be sure. BTW, what about the following?
Code:
MsgBox % rec(0, 1000)
return
rec(deep, end)
{
   Tooltip %deep%
   return (deep = end) ? "done" : rec(deep+1, end)
}
Back to top
View user's profile Send private message
Z_Gecko
Guest





PostPosted: Tue Aug 11, 2009 4:13 am    Post subject: Reply with quote

Quote:
BTW, what about the following?
Dies at 817 recursions.
Back to top
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Tue Aug 11, 2009 10:23 am    Post subject: Reply with quote

Sean is right, script function recursion is limited by available stack space.

Simple comparison of the esp register before and after entering ExpandExpression() in debug mode shows around 4.5KB of stack usage. Furthermore, temporary string values less than 4KB may be stored on the stack, up to a total of about 40K. This is per expression/function-call, before even beginning to evaluate the expression. Compare that to a compiled/machine code function call, which would use minimum 4 bytes (32 bits) on a 32-bit system.
Quote:
Dies at 817 recursions.
FYI, I got up to 818 on v1.0.48.03, 804 on my current custom build, and 287 on v1.0.47.06. The last one is probably because that version used much more stack space, as it had to convert the textual form of the expression into a tokenized form before every evaluation. Now this is done only once per expression, at load time. My current custom build uses a few more local variables than v1.0.48.03, so dies a little sooner.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group