CLOSED: too much recursion

0 favourites
  • 5 posts
  • Link to .capx file (required!):

    It's a simple test of recursion, it ought to calculate the sum of all integers up to the value I put into the function call parameter0

    Steps to reproduce:

    1. run the layout

    2. press the calculate button


    Observed result:

    About half the times, for values over 40 in the function call, it gives me this message:

    *Javascript error!

    too much recursion

    localhost/Function_plugin.js, line 192 (col undefined)

    This is either a bug in Construct 2 or a problem in a third party plugin or behavior - please report it to the developer"

    If I press the "calculate" button again, it DOES provide the correct calculation

    Expected result:

    It should provide the correct calculation without first giving me an error message.

    Browsers affected:

    Chrome: yes ( for values over 1000 )

    Firefox: yes (for values over 40)

    Internet Explorer: ?

    Operating system & service pack:

    windows 7

    Construct 2 version:

    128.2 (32bit)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Closing - despite the error message, this is not a bug in Construct 2. In all programming languages you cannot recurse too deep or you overflow the stack. For example with a value of 1000, you have 1000 simultaneous function calls, which exhausts the memory area reserved for function calls.

    This is a problem with recursion in any programming language. The solution is to use a loop instead, which is easy in this case.

  • thank you ashley

  • Deep recursion can commonly cause a stack overrun and it should be avoided in favor of loops whenever possible. I did notice something that could possibly be a problem though.

    I couldn't reproduce the 50/50 behavior you described with Chrome. With a value of 500, on average it would work for 4 clicks and then on the fifth and above it would continually produce a minimum call stack size exceeded error. Lower values would do the same, generally requiring more clicks. It sounds like each subsequent call to the function is pushing deeper into the stack and that doesn't seem like something that should be happening.

    I'm not a javascript programmer, so I don't how much of a problem this really is.

    EDIT: There seems to be a specific cut off for me where the problem simply doesn't happen and where it happens unconditionally somewhere around 408 levels deep. I don't think this is an issue until you hit the critical point.

  • Oh thank you DW, I just saw this.

    I will definitely use loops instead of deep recursion. This was an exercise for a procedurally generated graphics project. I just wanted to understand recursion.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)