Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    For loop variable weirdness that should not happen, is this a bug of fuze or from me ?

    General Discussion
    7
    15
    495
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • joyrider3774
      joyrider3774 last edited by joyrider3774

      Consider the following program, it is a shortened version of what was happening inside my program causing all sorts of corruption

      1857e912-aa13-46cd-8fb2-4d5af2896d62-image.png

      Can someone explain to me how the i for variable inside the main loop can become 15 after running about 4000-5000 iterations fine. It happens between iterations 4000 and 5000

      6cbd83e0-bf16-42a0-aa33-0c9335335651-image.png

      I know the fix though, give each for loop variable everywhere a unique name but that does not make sense at all

      I like to get some insights on this

      1 Reply Last reply Reply Quote 1
      • joyrider3774
        joyrider3774 last edited by

        Renaming all for loop variables to a unique name is not the solution either. I am greeted with the following after 14000 -15000 iterations, yet i am not explicitly creating any new variables that i see (i moved the sum declaration outside of the loop already compared to previous version) ..

        1b31f068-3340-42c1-9ea4-7f1bb5657848-image.png

        if you comment c = controls(0) the stackoverflow does not happen but i don't see how that would create a new variable given i created prevc and c already outside of the loop

        I have shared this test program for others to test / tell me what i did wrong or investigate. But still needs to be approved as currently in my real game i just renamed the for loop variables to be unique but this seems to be not really the solution if you leave it running longer

        16097b42-00c0-4601-ac34-4c8610d55f7f-image.png

        D 1 Reply Last reply Reply Quote 1
        • spikey
          spikey F last edited by

          As a user, I can only guess, about the internal whys. So, I try to stick to what I learnt and works to get on:

          • don't let you down on a few internal behaviours of the FUZE4 interpreter. There is really a lot that influenced it, that we don't know.
          • the good thing is, there is always (or with very high probability) a work around, until you really try out the things nobody did in the last 1.5 years

          To the things you observed: controls() need to be called with update(), somewhere further down in your code, see here:
          https://fuzearena.com/forum/post/2882

          About your for-loops: I try to avoid loops outside of the main loop. And put these initializations into functions. Inside functions you can always use the same for-variable 'i'. As you probably know, variables outside of the main loop, become global. And even I saw that the 'i' in a for loop is only available inside the loop. I still think the global scope of for loops, using 'i' and using that variable again in for-loops with a local scope inside a function, caused me troubles. Also I cannot easy reproduce this anymore. Avoiding it, saved me time.

          1 Reply Last reply Reply Quote 3
          • D
            doumdoum F @joyrider3774 last edited by

            @joyrider3774 Code for Line 21-22

            var c = controls(0)
            var prevc = controls(0)
            
            1 Reply Last reply Reply Quote 1
            • PB____
              PB____ last edited by PB____

              I think doumdoum might be right here, that has been an issue in Fuze.
              Does your program still crash if you leave out lines 21, 22, 27 and 28?

              1 Reply Last reply Reply Quote 0
              • JMM161437
                JMM161437 F last edited by

                When it comes to arrays you need to keep in mind that when you make one with 15 separate items they are indexed from 0-14 and not 1-15. so when you call out an array with 15 items like

                MyArray[15]

                you get an error because it'll try to index 15 which don't exist. Hopefully this helps

                1 Reply Last reply Reply Quote 0
                • joyrider3774
                  joyrider3774 last edited by joyrider3774

                  @spikey thanks creating an init function indeed fixes the weird index 15 out of bounds error with the for loop variable so thats ok. but about the update() call i already have an update call and it still crashes after about 14000-15000 iterations with the stackoverflow error

                  @JMM161437 i'm aware arrays are zero based, the issue here was the for loop always goes to end value -1 so in theory it could have never reached index 15 in the main loop to make the array go out of bounds, that seemed to be an issue on reusing same for loop variable names in global and local scope and it gets fixed by placing the global scope for loops inside a seperate function.

                  @doumdoum, @PB____ i've tried placing var in front of those variables but it made no difference. i've placed the global scope for loops inside an init function so that has no influence anymore and even placed the main loop inside a seperate function but the program still crashes with a stackoverflow saying memory usage for variables has been used up after about 14000 iterations. So i started commenting things. If i only comment c = controls(0) it crashes with that same error after 28000 iterations so it seems it's not only the c=controls(0) call that leads up to this issue. So i started commenting more things and see how many iterations it can go. I also commented all the sum variable usage and it still crashes after about 28000 iterations so the sum variable is not the culprit. I'm still investigating to see what (which variable / line) is causing this.

                  The original program is now live also, so you guys can try experiment as well with it although i have now changed the program already to whats shown below. I can also share that if needed but the initial example program is easily changed to what i have below now
                  b2fd572c-fcbd-4118-9926-7a6d6d9c613a-image.png

                  so even this program below crashes after 28000 iterations
                  990ec353-5e83-4d19-b19c-134a79790534-image.png

                  Edit: prevc = c in the loop above seems to be a big culprit commented it and i'm nearing iteration 100000 and no crash yet with only that change compared to last screenshot's code. But i've still got to see what will happen if i uncomment c = controls(0) again so will slowly uncomment things to see how far it can go. If this is really the single culprit and we can not really do this how can i compare controls from previous frame with current frame ? i'm basically doing that to make sure some button was not pressed in previous frame while it is pressed in current frame (like to make sure a single button press happens and not keep reacting on it every frame if (!prevc.a) and c.a then ...)

                  I'll keep you guys posted

                  1 Reply Last reply Reply Quote 0
                  • joyrider3774
                    joyrider3774 last edited by

                    ok i don't get it all ...

                    This crashes with the stackoverflow / max memory exceeded after about 28600 iterations

                    0ccdc106-8c5f-47ee-a7d0-67b2689bb4c6-image.png

                    Put the for loop in comments and it happily goes to 100000 without crashing (i stopped it then did not let it run further, but i guess i could test it by letting it run overnight to see if it actually never crashes)

                    7dc430c1-86d8-45e4-95a4-331b7703a861-image.png

                    And this makes no sense at all to me whats up with the for loops causing this ? I have one more idea to try and thats doing 2 update() calls after each other

                    1 Reply Last reply Reply Quote 0
                    • spikey
                      spikey F last edited by spikey

                      Quick note: enable the debug output of FUZE: if it grows, there is a memory leak, which will sooner or later crash. So, you dont have to let it run over night to see if it works.
                      Settings->Preferences->Show FPS + Memory Meter When Running Programs

                      Appendix: just saw that the memory does not change, or maybe very very slowly. Even after removing all sleeps and prints.

                      1 Reply Last reply Reply Quote 0
                      • Kat
                        Kat Fuze Team last edited by

                        @joyrider3774 I've just tried running the 2nd most recent version you posted (the one with lines 35 and 40 uncommented) on the latest patch and I've passed iteration 100,000 with no errors :) So hopefully when it's out the patch will solve a lot of the problems you've been having.

                        1 Reply Last reply Reply Quote 3
                        • spikey
                          spikey F last edited by spikey

                          I could reproduce a software crash, when running your latest version, after 2.5 min. Right now, you have the variable definition c = controls(0) inside main() this is a local scope. Now, its not clear for me, why this is still a problem, because, it looks like it could be "re-used" (my latest wild guess: its not re-used anyway, it allocates new memory and copies the content to it and replaces the old reference to the data with the new reference. Would be interesting to hear what you think nowadays) in your loop in 30-43. But it seems to be necessary (in FUZE version 2.15.0) to have c = controls(0) at the global scope (apology, for my cheeky proposition). So, if you take out c = controls(0)from your main()and put it between line 46 and 47, it will not crash anymore.

                          1 Reply Last reply Reply Quote 0
                          • joyrider3774
                            joyrider3774 last edited by

                            @Kat thats good to hear will definatly retest it all when the patch comes out.
                            @spikey will test it. I just changed the code and started another run and hope it won't crash again (it already passed the 29000 mark so thats a good thing but will see how long it goes, i can leave it running for a few hours and hope it did not crash). Ps its exactly what i was thinking also that the variable is not reused and new memory (for variables) keeps being allocated up everytime it passes that line until it runs out of variable memory space

                            About the 1st version in my previous post (with the for loop commented) it happily goes to 1800000 without a problem (left it running during the day) while the second version (with the for loop uncommented) always crashed at iteration 28600 am running it now again with @spikey suggestion to make that line global scope and will see how it goes. But it is kinda weird that an empty for loop has an impact on it. I'll keep you guys posted

                            Oh on a side note i always run with the debugging enabled but don't see the memory going down during all of this not sure it shows / includes the variable memory space, maybe that memory is something else shown ? (like video memory for tiles / images and such)

                            1 Reply Last reply Reply Quote 2
                            • joyrider3774
                              joyrider3774 last edited by joyrider3774

                              Hello guys,

                              So final testings. @spikey 's suggestion to place it a global scope indeed fixed the problem. Initially i only placed the initial call (var c = controls(0)) at global scope with the for loop enabled but not the contents. It went up to 1 million iterations without a problem. Next i added also var prevc = c below it and added the same statement inside the loop. I left it running fine til 100000 iterations without a problem. And in my final test i enabled the loop contents again and this also seemed to run fine up until 70000 iterations. Maybe tonight when i go to bed i let it run during the night as a real final test.

                              So what have we learned ?

                              • Never use any for loops at global scope, place them inisde (init) functions or you will run into trouble if you use the same for variable from global scope in a local scope for loop after some time
                              • Never place (define) var c= controls(0) at local scope place it in global scope or you will run into a stackoverflow eventually saying that variable space has been exhausted you can ofcourse in your main loop (at local scope) do c = controls(0)again to update the c value
                              • you can remember the previous frame controller state (for comparing it with the state in the current frame) by doing first var c = controls(0) and right below it add var prevc = c at global scope then you can add in your loop (at local scope) the same statements without the var paremeter but place the prevc = c call before the c = controls(0) I did test using functions only did not retest it all without functions so best would be to use functions anyways to prevent problems just in case.

                              Here was the final version of the program i was testing and it's exactly what i need for my game. In case anyone is wondering that Z value calculation is to create the Z position value for infinite road (or any other repeating tile).

                              d52c25fe-204f-4dcd-a3ec-782be14bc426-image.png

                              What started as seeing corruption in a game i was making ended up becomming one big investigation.

                              I will definatly restest this once the new patch is released as it seems by @Kat 's observations it did not crash with the new version at points where it did crash with me (on 2.15.0) during the testing. (for example when defining the var c = controls(0) at local scope (inside a function before using it in a loop)

                              i now have to change some code in my znax game also to prevent these problems from occuring.

                              I hope it helps someone and Thanks to all involved

                              PS. i will edit the post tommorow after i let it ran during the night to make sure it really did not crash the final program i only left going till iteration 70000 but i got a good feeling about it now

                              Edit: 500000+ iterations with last code posted (oh yeah seems i spelled iteration wrong ;))
                              0a0d3327-3437-456e-ae02-1e5fd2598d52-image.png

                              1 Reply Last reply Reply Quote 2
                              • Willpowered
                                Willpowered Fuze Team last edited by

                                I know @Kat already gave it a test, but I just want to confirm that this is a bug I worked on and indeed fixed for the upcoming patch.

                                1 Reply Last reply Reply Quote 4
                                • joyrider3774
                                  joyrider3774 last edited by joyrider3774

                                  @Willpowered would that be both problems ? meaning the weird array index (for loop value) you can get after a while if you reuse the same name for a for loop variable in global and local scope and the stackoverflow with exhausted variable space when using c=controls(0) in local scope, or only the last one ? The test kat did, used a workaround for the 1st problem by not using for loops at global scope and placing them in an init function so i'm not sure that issue is also addressed (see very first post for example code that triggered this after about 5000 iterations)

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post