Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Debugging potential infinite loop or unity bug. (C#, Visual studio)

Discussion in 'Scripting' started by ben199131837575, Jan 20, 2021.

  1. ben199131837575

    ben199131837575

    Joined:
    Mar 25, 2020
    Posts:
    4
    At a particular point while my project is running the unity editor freezes and I have to use task manager to exit and reload.

    I am using visual studio to debug my code. When the program freezes, and I try to find the code, VS tells me there is no compatible code to view. How exactly can I overcome this. It is not clear in my code how a infinite loop could occur, but there is potentially a lot of code that is run and so is possible that I missed it.

    I have added debug.logerrors and breakpoints in all the loops I am confident the code can reach and nothing occurs.

    If I try to follow the error by putting the break point higher it seems that my code finishes and goes onto run some unity stuff. Either it is happening there or it is happening at the start of the next frame before my code runs (at least fairly sure it is before my code starts as it never reaches my breakpoints)

    I have been stuck with this for months now, and I am at the point where I cannot continue without resolving this. An ideas of how to tackle this will be appreciated.

    Thank you.

    Ben.
     
  2. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38
    Do you know what code you were working on before the freezes? If it is an infinite loop causing the freeze, it's gonna be hard to help you without seeing it.
     
    Rugbug_Redfern likes this.
  3. ben199131837575

    ben199131837575

    Joined:
    Mar 25, 2020
    Posts:
    4
    There is a lot of code that could potentially be running before it (few 1000 lines on multiple scripts). I don't really want to spend hours clicking next on debugger to get to it. This is my predicament. I am looking into improving my skills with the debugger to see if I can get closer to the code.
     
  4. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38
    Fair enough, unfortunately I can't help you improve your debug skills, I'm sure someone here could.

    But, you didn't write 1000 lines of code before testing did you? You can't remember the last bit you worked on before it happened?
     
  5. ben199131837575

    ben199131837575

    Joined:
    Mar 25, 2020
    Posts:
    4
    It is a simulated world that AI act upon and so there is many possible scenrios that can occur. I am sure there is a way to test something like my game, but I would not know where to start.

    I am pretty confident though that the freezing/infinite loop is occurring in unity. I am trying to downgrade my project to 2018 to see if that makes a difference.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Don't do that. You are likely to permanently damage parts of your project.

    First, put everything into source control. git works great with Unity and there's tons of tutorials.

    Once it is all safely source controlled, now begin your cruel scientific experiments on the code to find the crash.

    By "cruel scientific experiments" I mean broadly damaging and wrecking the code to remove the use of certain features. Particularly look for features that do things like looping over collections or scanning lists of items. Make those things fail (or artificially immediately succeed with dummy values).

    Each change you make, run the game and see if it still locks up.

    Fairly quickly you will make a change that removed the lockup. Now you have a place to start looking closer. Why does that change fix the lockup?
     
  7. ben199131837575

    ben199131837575

    Joined:
    Mar 25, 2020
    Posts:
    4
    Cracked it XD.

    Go to you visual studio application (after you attach to unity, press play in unity, *set of actions to recreate*, freezes ect) and find the window for threads . From there you can click on the thread that is running your code. Easy. Duno why it would not just guess that you want to see your code, but maybe VS cannot tell the difference.

    Got the solution from:
    https://answers.unity.com/questions/1429628/cant-find-loop-that-freezes-unity.html

    Code (CSharp):
    1.  
    2. bool maxguernseyiii_is_lifesave()
    3. {
    4.     if(1 == 1){
    5.        return true;
    6.     }
    7.     return true;
    8. }
     
    Whatever560 likes this.